Simultaneous detection of Pinch and Rotation gestures

I have successfully implemented gestures that allow users to zoom in and out on a view using UIGuestureRecognizers. However, the user cannot perform two gestures at the same time (i.e., rotate and zoom at the same time). How can i do this? Below is how I added gestures

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

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

      

+3


source to share


3 answers


In swift 3, the delegate method name is:

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

      



Also you need to set up a delegate for gestures:

rotateRecognizer.delegate = self
pinchRecognizer.delegate = self

      

+4


source


Just added this and it works:



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

      

+3


source


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

self.imageView.addGestureRecognizer (rotateGesture)

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

      

+1


source







All Articles