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

标签 ios uiview uigesturerecognizer zooming uipangesturerecognizer

我有一个 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];
}

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

最佳答案

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

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

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

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

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

- (void)handlePan:(UIPanGestureRecognizer *)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/

相关文章:

ios - 带有平移手势识别器的 CollectionViewCell

ios - 使用 UIGestureRecognizer 同时缩放、旋转和移动

ios - 如何在没有来自 MFi 程序的协议(protocol)的情况下使用外部附件框架

iphone - 从 SWF 到 AIR 再到 iOS 应用程序

iphone - 不进行dealloc操作

ios - UIView 使用约束自动布局居中和调整大小

ios - 像 Mail App 这样的 UITextView 的滑动/拖动手势

ios - 错误域=EKCADErrorDomain 代码=1013 "The operation couldn’ t 完成。 (EKCADErrorDomain 错误 1013。)”

ios - 如何获取ios中所有UTC缩写的列表?

ios - 检查以编程方式创建的 UIView 是否可见