Detacting Pinch and Rotation gestures simultaneously

3.7k views Asked by At

I've successfully implemented gestures that allow users to enlarge and rotate a view using UIGuestureRecognizers. However, the user can't do two gestures at the same time (i.e. rotate and scale at the same time). How can I go about doing that? Below is how I added the gestures

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

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

There are 3 answers

0
SpaceShroomies On

Just added this and it works:

func gestureRecognizer(UIGestureRecognizer,
        shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool {
            return true
    }
0
Bohdan Savych On

In swift 3 the delegate method name is:

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

Also you need set delegate for gestures:

rotateRecognizer.delegate = self
pinchRecognizer.delegate = self
0
Sai kumar Reddy On

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