3

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?

Q.u.a.n.g L.
  • 1,564
  • 1
  • 12
  • 27

2 Answers2

8

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.

Valent Richie
  • 5,226
  • 1
  • 20
  • 21
0

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.

Fattie
  • 27,874
  • 70
  • 431
  • 719