• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS - 设置平移的最小/最大限制

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:45:58 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 UIViewController,它调用了一个 UIView:

我正在使用 UIPinchGesture 放大 UIView 我想要做的是根据缩放比例限制用户可以平移多少

即 “当前规模”

目前我使用的代码不允许平移,当 currentScale(缩放的数量)小于 1.1 倍缩放时,但如果 1.1 很棒,它允许平移,但这允许 UIView 被平移和移动没有界限,我希望能够将其平移量设置为其边界:当前代码

if (currentScale <= 1.1f) {
    // Use this to animate the position of your view to where you want
    [UIView animateWithDuration: 0.5
                          delay: 0
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         CGPoint finalPoint = CGPointMake(self.view.bounds.size.width/2,
                                                          self.view.bounds.size.height/2);
                         recognizer.view.center = finalPoint; }
                     completion:nil];
}

else {
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:self.view];
}

一些方向,将不胜感激 - 谢谢!



Best Answer-推荐答案


免责声明 - 这可能不是最好的方法,但这就是我解决它的方法:

1) 我通过测量 View 中心如何偏离其原始位置来推断我需要在 5 个不同的缩放点在 X 0r Y 方向上平移多少:

2)我使用 NSLog 进行大部分测量)-我对结果进行了标准化-并将其绘制在 excel 中-绘制了一条曲线-并得到了缩放级别与 View.center 的方程式

3) 然后我根据我得到的方程式简单地对平移手势进行编码:

代码如下(xMax, xMin, yMax, yMin 都绘制了公因数为“zoomScale”的方程

- (void)handlePanUIPanGestureRecognizer *)recognizer {

//dont pan if zoomscale = 1 (this indicates no zooming)
if (zoomScale <= 1.0f) {
    return;
}

//panning gesture began / state changes 
if ([recognizer state] == UIGestureRecognizerStateBegan ||
    [recognizer state] == UIGestureRecognizerStateChanged) {

    //detect translation gesture 
    translation = [recognizer translationInView:self.view];
    //newCenter is a variable detecting how your translation gesture would efect your view's center 
    CGPoint newCenter = CGPointMake(recognizer.view.center.x + translation.x,
                                    recognizer.view.center.y + translation.y);

    //Check whether boundary conditions are met 
    BOOL inBounds = (newCenter.y >= yMin && newCenter.y <= yMax &&
                     newCenter.x >= xMin && newCenter.x <= xMax);

    if  (inBounds) {
        //if boundary conditions met : translate your view 
        recognizer.view.center = newCenter;
        [recognizer setTranslation:CGPointZero inView:self.view];
    }
} 

希望这可以帮助那里的人:不是你必须声明你必须在你的 viewDidLoad 方法中启动(声明)UIPanGestureRecognizer 才能工作

关于iOS - 设置平移的最小/最大限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18381482/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap