Q.u.a.n.g L.
Q.u.a.n.g L.

Reputation: 1614

Conflict between pan and swipe gesture in the same view?

Is there anyway to distinguish pan and swipe gesture in the same view? I have 2 gestures work on a same view simultaneously by using the delegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

I did set the pan gesture's property minimumNumberOfTouches = 1. The problem is when I'm panning, the swipe gesture is triggered. How to make swipe gesture stop when I'm in panning process?

Upvotes: 3

Views: 3925

Answers (2)

Fattie
Fattie

Reputation: 12598

Next-decade solution!

override func viewDidLoad() {
    super.viewDidLoad()
    
    let s = UISwipeGestureRecognizer(target: self, action: #selector(bye))
    s.direction = .down
    view.addGestureRecognizer(s)
    
    let p = UIPanGestureRecognizer(target: self, action: #selector(pan))
    p.require(toFail: s)
    view.addGestureRecognizer(p)
}

It's that simple now. Both will work.

Upvotes: 0

Valent Richie
Valent Richie

Reputation: 5226

Try to call the requireGestureRecognizerToFail: method in your swipe gesture

[swipeGestureRecognizer requireGestureRecognizerToFail:panGestureRecognizer];

This should cause the pan gesture to cancel the swipe gesture if the pan gesture is recognized or began.

Upvotes: 8

Related Questions