2

我在我的应用程序中使用 UIPinchGestureRecognizer 来放大视图(是的,我不使用 UIScrollView 是有原因的)。当我用手指向外捏合时,视图会按预期放大,如果我在不将手指从屏幕上移开的情况下反向捏合,它也会向右缩放。但是,如果我通过向内捏来启动缩放,则视图缩放的速度会大大降低。我猜这是因为 UIPinchGestureRecognizer 的工作原理 - 向外捏时 UIPinchGestureRecognizer 的比例为 >1,向内捏时为 <1。不幸的是,我不知道如何在我的代码中准确地反映这一点。

- (IBAction)didDetectPinchGesture:(id)sender {
    UIPinchGestureRecognizer *gestureRecognizer = (UIPinchGestureRecognizer *)sender;

    CGFloat scale = [gestureRecognizer scale];

    switch ([gestureRecognizer state]) {
        case UIGestureRecognizerStateBegan:
            _lastScale = [gestureRecognizer scale];
            break;

        case UIGestureRecognizerStateChanged:

            CGFloat currentScale = [[self.imageView.layer valueForKeyPath:@"transform.scale"] floatValue];

            // Constants to adjust the max/min values of zoom
            const CGFloat kMaxScale = 5.0;
            const CGFloat kMinScale = 1.0;

            CGFloat newScale = 1 -  (_lastScale - scale); // new scale is in the range (0-1)
            newScale = MIN(newScale, kMaxScale / currentScale);
            newScale = MAX(newScale, kMinScale / currentScale);

            NSLog(@"%f", newScale);

            CGAffineTransform transform = CGAffineTransformScale([self.imageView transform], newScale, newScale);
            self.imageView.transform = transform;

            _lastScale = scale;  // Store the previous scale factor for the next pinch gesture call
            break;

        default:
            _lastScale = [gestureRecognizer scale];
            break;
    }
}
4

1 回答 1

-1

gestureRecognizer scale一个非常简单的解决方案是在完成后将 back重置为 1:

    ...
    default:
        _lastScale = [gestureRecognizer scale];
        // Add this:
        [gestureRecognizer setScale:1];
        break;
    }
于 2014-03-05T07:39:48.310 回答