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

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


“currentScale”

当前,我使用的代码不允许平移,当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)我通过测量视图中心偏离其原始位置的方式,推论需要在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才能起作用

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

10-13 04:01