SpaceShroomies
SpaceShroomies

Reputation: 1635

Detacting Pinch and Rotation gestures simultaneously

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)

Upvotes: 5

Views: 3818

Answers (3)

Sai kumar Reddy
Sai kumar Reddy

Reputation: 1829

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

Upvotes: 3

Bohdan Savych
Bohdan Savych

Reputation: 3457

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

Upvotes: 7

SpaceShroomies
SpaceShroomies

Reputation: 1635

Just added this and it works:

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

Upvotes: 3

Related Questions