Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simultaneous gesture recognition for specific gestures

Tags:

I'm trying to enable simultaneous gesture recognition but only for the UIPinchGestureRecognizer and UIRotationGestureRecognizer gestures. I don't want it to work for any other gestures. If I set the following property to true it allows all gestures to be recognized simultaneously, how can I limit it to just rotating and scaling?

func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
like image 440
SpaceShroomies Avatar asked Jun 14 '15 13:06

SpaceShroomies


People also ask

What is Pan gesture?

A pan gesture occurs any time the user moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.

What is Cancelstouchesinview?

A Boolean value affecting whether touches are delivered to a view when a gesture is recognized.

What is gesture recognizer in Swift?

The tap gesture recognizer appears in the Document Outline on the left. We need to implement an action that defines what happens when the user taps the image view. Open ViewController. swift and define a method with name didTapImageView(_:) . It accepts the tap gesture recognizer as its only argument.


1 Answers

Make sure your class implements UIGestureRecognizerDelegate

class YourViewController: UIViewController, UIGestureRecognizerDelegate ...

Set the gesture's delegate to self

yourGesture.delegate = self

Add delegate function to your class

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if (gestureRecognizer is UIPanGestureRecognizer || gestureRecognizer is UIRotationGestureRecognizer) {
        return true
    } else {
        return false
    }
}
like image 85
Bannings Avatar answered Oct 18 '22 07:10

Bannings