UIPinchGestureRecognizer向内捏“慢”

时间:2012-06-23 14:39:04

标签: ios ios5 uigesturerecognizer cgaffinetransform uipinchgesturerecognizer

我在我的应用程序中使用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;
    }
}

1 个答案:

答案 0 :(得分:-1)

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

    ...
    default:
        _lastScale = [gestureRecognizer scale];
        // Add this:
        [gestureRecognizer setScale:1];
        break;
    }