同时检测捏合和旋转手势

Posted

技术标签:

【中文标题】同时检测捏合和旋转手势【英文标题】:Detacting Pinch and Rotation gestures simultaneously 【发布时间】:2015-06-10 07:56:08 【问题描述】:

我已经成功实现了允许用户使用 UIGuestureRecognizers 放大和旋转视图的手势。但是,用户不能同时做两个手势(即同时旋转和缩放)。我该怎么做呢?下面是我添加手势的方法

var rotateRecognizer = UIRotationGestureRecognizer(target: self, action: "handleRotate:")
var pinchRecognizer = UIPinchGestureRecognizer(target: self, action: "handlePinch:")

testV.addGestureRecognizer(rotateRecognizer)
testV.addGestureRecognizer(pinchRecognizer)

【问题讨论】:

***.com/questions/8108768/… 【参考方案1】:

在 swift 3 中,委托方法名称是:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool  
        return true
    

您还需要为手势设置委托:

rotateRecognizer.delegate = self
pinchRecognizer.delegate = self

【讨论】:

【参考方案2】:

刚刚添加了这个,它就可以工作了:

func gestureRecognizer(UIGestureRecognizer,
        shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool 
            return true
    

【讨论】:

【参考方案3】:

让 rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(self.rotateGesture))

self.imageView.addGestureRecognizer(rotateGesture)

让 pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.pinchGesture)) self.imageView.addGestureRecognizer(pinchGesture)

func rotateGesture(sender: UIRotationGestureRecognizer)
    sender.view?.transform = (sender.view?.transform)!.rotated(by: sender.rotation)
    sender.rotation = 0
    print("rotate gesture")

func pinchGesture(sender: UIPinchGestureRecognizer)
    sender.view?.transform = (sender.view?.transform)!.scaledBy(x: sender.scale, y: sender.scale)
    sender.scale = 1
    print("pinch gesture")

【讨论】:

以上是关于同时检测捏合和旋转手势的主要内容,如果未能解决你的问题,请参考以下文章

如果视图注册平移手势,则捏合手势不起作用

带有平移、旋转和捏合手势的 UIKit Dynamics

手势识别(拖动,旋转,捏合)

使用平移、旋转和捏合手势编辑后如何保存 UIImage

iOS 手势操作:拖动捏合旋转点按长按轻扫自定义

Angular 2 RC 2 使用移动手势(滑动、捏合、旋转)