捏手势IOS的手势绑定限制

Posted

技术标签:

【中文标题】捏手势IOS的手势绑定限制【英文标题】:Gesture Binding limit for pinch gesture IOS 【发布时间】:2016-08-11 10:25:11 【问题描述】:

我正在使用捏合手势来缩小图像VIew 现在我可以将向下的限制设置为不会让用户缩小的限制。我正在使用下面的代码。

func handlePinch(recognizer : UIPinchGestureRecognizer)
    
        imageView.transform = CGAffineTransformScale(imageView.transform,
                                                         recognizer.scale, recognizer.scale)

        recognizer.scale = 1.0

【问题讨论】:

【参考方案1】:

我已经使用一些静态变量完成了这项工作

var cumulativeScale:CGFloat = 1.0
var maxScale:CGFloat = 3.5
var minScale:CGFloat = 0.5

@IBAction func zoomingInAndOut(_ pinchGesture: UIPinchGestureRecognizer) 
        guard pinchGesture.view != nil else return

        if pinchGesture.state == .began || pinchGesture.state == .changed
            if (cumulativeScale < maxScale && pinchGesture.scale > 1.0) 
                pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)
                cumulativeScale *= pinchGesture.scale
                pinchGesture.scale = 1.0
            
            if (cumulativeScale > minScale && pinchGesture.scale < 1.0) 
                pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: pinchGesture.scale, y: pinchGesture.scale)
                cumulativeScale *= pinchGesture.scale
                pinchGesture.scale = 1.0
            
        
    

【讨论】:

【参考方案2】:
(void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer 

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) 
        // Reset the last scale, necessary if there are multiple objects with different scales
        lastScale = [gestureRecognizer scale];
    

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || 
        [gestureRecognizer state] == UIGestureRecognizerStateChanged) 

        CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

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

        CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]); 
        newScale = MIN(newScale, kMaxScale / currentScale);   
        newScale = MAX(newScale, kMinScale / currentScale);
        CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
        [gestureRecognizer view].transform = transform;

        lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call  
    

【讨论】:

【参考方案3】:

没有直接方法来限制 UIPinchGestureRecogniser 的规模。 但是您可以尝试这里描述的技术:

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

【讨论】:

我已经确定这是可能的 您愿意分享您的策略吗?

以上是关于捏手势IOS的手势绑定限制的主要内容,如果未能解决你的问题,请参考以下文章

在iOS中的iPad上捏缩放将图像移动到最左角?

iOS 捏合手势与滑动手势冲突

在 Swift Playground 中捏手势?

捏手势不适用于 SKCameraNode

滚动视图平移防止捏手势?

找捏手势的点