2

我有一个UIViewController,它调用一个UIView:的iOS - 设置最小/最大限制平移

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

即 “currentScale”

目前我使用的代码允许无平移,当currentScale(金额放大)小于1.1倍变焦,但如果它是伟大的,1.1它允许pannin,但这允许UIView被平移和移动无边界,我希望能够et panning amount to its boundaries:当前代码

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]; 
} 

某些方向,将非常感激 - 谢谢!

+0

您正在使用平移还是捏手势? –

+1

我认为这可能有助于你。它对我来说很有用。 http://stackoverflow.com/questions/1362718/scroll-a-background-in-a-different-speed-on-a-uiscrollview –

+0

@pratyusha - 我使用两个,Rajpuroht - 这是为了UIScrollView:讨论速度,而不是限制空间 –

回答

1

免责声明 - 这可能不是这样做的最佳方式,但是这是我如何解决它:

1)我推断我是多么需要在5个不同的X 0R Y方向平移通过测量视图中心偏离其原始位置的方式来确定缩放点:

2)我使用NSLog进行大部分测量) - 我对结果进行了标准化 - 并将其绘制在excel中 - 绘制出曲线 - 并得到一个公式缩放级别Vs 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以使其工作

相关问题