Is there any priority condition between gesture methods (Pan Gesture and Swipe Gesture)?
S

3

13

I am developing an application where I have used the Pan Gesture as well as Swipe Gesture. So every time I do the Swipe Gesture but the method from the Pan Gesture is always getting called and Swipe Gesture method is not getting called.

Is there any priority between all the gesture method?

Shuttering answered 2/2, 2011 at 13:55 Comment(0)
D
15

You can call them in parallel by implementing the following method of the UIGestureRecognizerDelegate protocol:

- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
Displode answered 14/4, 2012 at 13:33 Comment(2)
I'm using "github.com/XavierDK/XDKAirMenu" and also am using swipe gesture in one of my controller so this delegate is not working for meBakeman
I tried this with the NSPanGestureRecognizer and the NSClickGesture recognizer and it did not work for me. My NSButton click only get through about 25% of the time unless I disable the pan recognizer.Pneumonia
S
9

There is a property on the UIGestureRecognizer class called "cancelsTouchesInView" which defaults to YES. This will cause any pending gestures to be canceled. The Pan gesture is getting recognized first since it does not need to have a "touch up" event, so it cancels the Swipe gesture.

If you want both gestures to be recognized, try adding:

[yourPanGestureInstance setCancelsTouchesInView:NO];

Skirl answered 2/2, 2011 at 23:16 Comment(2)
thank You for The answering But This Method Still don't Identify the Swipe GestureShuttering
I have Also Try The shouldRecognizeSimultaneouslyWithGestureRecognizer Method Within the UIGestureRecognizer.H fileShuttering
A
3

Give priority to swipe

You can give priority to a UIGestureRecognizer with the require(toFail:) method.

@IBOutlet var myPanGestureRecognizer: UIPanGestureRecognizer!
@IBOutlet var mySwipeGestureRecognizer: UISwipeGestureRecognizer!

myPanGesture.require(toFail: mySwipeGestureRecognizer)

Now your pan will only execute if your swipe fails.


Use pan for everything

If the swipe and pan gesture recognizers don't play nicely with this setup, you can roll all of your logic into the pan gesture recognizer for more control.

let minHeight: CGFloat = 100
let maxHeight: CGFloat = 700
let swipeVelocity: CGFloat = 500
var previousTranslationY: CGFloat = 0

@IBOutlet weak var cardHeightConstraint: NSLayoutConstraint!

@IBAction func didPanOnCard(_ sender: Any) {

    guard let panGesture = sender as? UIPanGestureRecognizer else { return }

    let gestureEnded = bool(panGesture.state == UIGestureRecognizerState.ended)
    let velocity = panGesture.velocity(in: self.view)

    if gestureEnded && abs(velocity.y) > swipeVelocity {
        handlePanOnCardAsSwipe(withVelocity: velocity.y)
    } else {
        handlePanOnCard(panGesture)
    }
} 

func handlePanOnCard(_ panGesture: UIPanGestureRecognizer) {

    let translation = panGesture.translation(in: self.view)
    let translationYDelta = translation.y - previousTranslationY

    if abs(translationYDelta) < 1 { return } // ignore small changes

    let newCardHeight = cardHeightConstraint.constant - translationYDelta

    if newCardHeight > minHeight && newCardHeight < maxHeight {
        cardHeightConstraint.constant = newCardHeight
        previousTranslationY = translation.y
    }

    if panGesture.state == UIGestureRecognizerState.ended {
        previousTranslationY = 0
    }
}

func handlePanOnCardAsSwipe(withVelocity velocity: CGFloat) {
    if velocity.y > 0 {
        dismissCard() // implementation not shown
    } else {
        maximizeCard() // implementation not shown
    }
}

Here's a demo of the above code in action.

enter image description here

Avion answered 13/1, 2018 at 0:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.