同时解除捏合和旋转手势

时间:2015-06-10 07:56:09

标签: swift uigesturerecognizer

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

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

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

3 个答案:

答案 0 :(得分:7)

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

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

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

rotateRecognizer.delegate = self
pinchRecognizer.delegate = self

答案 1 :(得分:3)

刚刚添加了这个并且它有效:

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

答案 2 :(得分:2)

let 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")
}