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

后端 未结 10 1103
[愿得一人]
[愿得一人] 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:01

    - (void)pinchToZoom:(UIPinchGestureRecognizer*)gesture
    {
        switch (gesture.state)
        {
            case UIGestureRecognizerStateBegan:
            {
                lastScale = gesture.scale;
            }break;
            case UIGestureRecognizerStateChanged:
            {   
                const CGFloat zoomSensitivity = 5;
                const CGFloat zoomMin = 1;
                const CGFloat zoomMax = 16;
    
                CGFloat objectScale = gesture.view.contentScaleFactor;
                CGFloat zoomDiff = lastScale - gesture.scale;
                CGFloat zoomDirty = objectScale - zoomDiff * zoomSensivity;
                CGFloat zoomTo = fmaxf(zoomMin, fminf(zoomDirty, zoomMax));
    
                // step round if needed (neutralize elusive changes)
                zoomTo = (NSInteger)(zoomTo * 10) * 0.1;
    
                if ( objectScale != zoomTo )
                    gesture.view.contentScaleFactor = zoomTo;
    
                lastScale = gesture.scale;
            }break;
            default:
                break;
        }
    }
    

提交回复
热议问题