If we set gesture recognizer dependency A require failure of B, does it mean recognizer A won't receive event before B fail?

Viewed 401

https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate/1624229-gesturerecognizer For example, if I set the delegate of gesture recognizer A and return true when the otherGestureRecognizer is gesture recognizer B.

optional func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, 
         shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool

Does it mean gesture recognizer A won't even receive event like touchBegan, touchMoved or touchEnded until recognizer B fails? Or it will still function as normal, just the result of A's recognized is only delivered when B fails?

1 Answers

Even if one gesture recognizer requires another to fail, it may still receive touch events (e.g. touchesBegan, touchesMoved, etc.) before the gesture is recognized.

That having been said, the idea of gesture recognizers is to abstract us away from these pre-recognized touch events. Most of the time, we are using the existing, concrete gesture recognizers (e.g. UITapGestureRecognizer, UILongPressGestureRecognizer, etc.). As such, we don't generally deal with the underlying touch events, but rather specify a target/selector (or hook up an @IBAction in IB) for the method that will be called once the gesture is recognized. The appropriate action method is only called once the gesture is recognized, meaning that only a single gesture recognizer will be recognized. E.g., if A requires B to fail, then you will see either the action for A called, or B’s, but not both.

So, if implementing our own custom gesture recognizer, we will process touch events (including before the gesture has been recognized), but generally we will just use the gesture recognizer’s action, and will see one or the other.

Needless to say, rather than requiring one to fail, you can alternatively specify that simultaneous gestures can be recognized, with gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:). So, one can have either behavior, depending upon your app’s needs.

Related