Max/Min Scale of Pinch Zoom in UIPinchGestureRecognizer - iPhone iOS

后端 未结 10 1107
[愿得一人]
[愿得一人] 2020-11-29 17:29

How would I be able to limit the scale of the UIPinchGestureRecognizer to a min and max level? The scale property below seems to be relative to the last known scale (the de

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 18:13

    Other approaches mentioned here did not work for me, but taking a couple things from previous answers and (in my opinion) simplifying things, I've got this to work for me. effectiveScale is an ivar set to 1.0 in viewDidLoad.

    -(void)zoomScale:(UIPinchGestureRecognizer *)recognizer
    {
        if([recognizer state] == UIGestureRecognizerStateEnded) {
            // Reset last scale
            lastScale = 1.0;
            return;
        }
    
        if ([recognizer state] == UIGestureRecognizerStateBegan ||
        [recognizer state] == UIGestureRecognizerStateChanged) {
    
            CGFloat pinchscale = [recognizer scale];
            CGFloat scaleDiff = pinchscale - lastScale;
    
            if (scaleDiff < 0)
                scaleDiff *= 2; // speed up zoom-out
            else
                scaleDiff *= 0.7; // slow down zoom-in
    
            effectiveScale += scaleDiff;
            // Limit scale between 1 and 2
            effectiveScale = effectiveScale < 1 ? 1 : effectiveScale;
            effectiveScale = effectiveScale > 2 ? 2 : effectiveScale;
    
            // Handle transform in separate method using new effectiveScale    
            [self makeAndApplyAffineTransform];
            lastScale = pinchscale;
        }
    }
    

提交回复
热议问题