In a UIKit note app, a horizontal pan on a card inside a vertical UIScrollView either blocks scrolling or never starts; how do you settle the conflict between the two recognizers?
answer
- two pans watch the same finger
- one recognizing blocks the other
- decide early, on the first movement
- require(toFail:) on panGestureRecognizer
- simultaneous recognition is rarely the fix
basics
~20 sThe card's pan and the scroll view's panGestureRecognizer both see the touch, and by default one recognizing blocks the other. Begin the card's pan only for mostly horizontal drags in gestureRecognizerShouldBegin(_:), and make the scroll view's pan require(toFail:) it.
solid answer
~40 sHit testing puts both recognizers in play: the card's `UIPanGestureRecognizer` is on the hit-test view and the scroll view's `panGestureRecognizer` is on an ancestor. Unless a delegate allows simultaneous recognition, one recognizing blocks the other, so whichever starts first wins regardless of direction. Make the decision explicit. In the card pan's delegate, `gestureRecognizerShouldBegin(_:)` returns `true` only when `velocity(in:)` is mostly horizontal; returning `false` moves the pan to `.failed`. Then call `scrollView.panGestureRecognizer.require(toFail: cardPan)`: the scroll view's pan waits in `.possible` until the card's pan fails, and fails itself if the card's pan begins. Returning `true` from `gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)` instead lets the card slide while the list scrolls, which is usually the bug you started with.
code
swift · 33 linesimport UIKit
final class NoteCardsViewController: UIViewController, UIGestureRecognizerDelegate {
let scrollView = UIScrollView()
let card = UIView()
override func viewDidLoad() {
super.viewDidLoad()
let swipe = UIPanGestureRecognizer(target: self, action: #selector(handleSwipe(_:)))
swipe.delegate = self
card.addGestureRecognizer(swipe)
// The scroll view's own pan waits until the card's pan has failed.
scrollView.panGestureRecognizer.require(toFail: swipe)
}
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
guard let pan = gestureRecognizer as? UIPanGestureRecognizer else { return true }
let velocity = pan.velocity(in: card)
// Begin only for a mostly horizontal drag; a vertical one fails at once.
return abs(velocity.x) > abs(velocity.y)
}
@objc func handleSwipe(_ pan: UIPanGestureRecognizer) {
switch pan.state {
case .began, .changed:
card.transform = CGAffineTransform(translationX: pan.translation(in: scrollView).x, y: 0)
case .ended, .cancelled:
UIView.animate(withDuration: 0.25) { self.card.transform = .identity }
default:
break
}
}
}go deeper
Know that a scroll view has its own panGestureRecognizer and that it competes with any pan recognizer you add to a view inside it.
Explain the states of a continuous recognizer and that, by default, one recognizer recognizing blocks the other.
Settle the conflict deliberately: gestureRecognizerShouldBegin(_:) decides direction, require(toFail:) orders the recognizers, delegate failure requirements cover recognizers you do not own, and every action handles cancelled.
Keep gesture ownership simple in shared components, documenting which recognizer must fail first, so nested scrolling screens built by different people do not stack conflicting recognizers.
## Why the two pans fight A notes app shows cards in a vertical `UIScrollView`. Each card can be swiped sideways to archive it with a `UIPanGestureRecognizer`. Depending on timing, either the card grabs vertical drags so the list will not scroll, or the list grabs every drag so the card never moves. Hit testing finds the card, and gesture recognizers handle touches hit-tested to their view **and its subviews**, so two recognizers watch the same finger: - the card's own pan recognizer, on the hit-test view; - the scroll view's `panGestureRecognizer`, on an ancestor. Both are **continuous** recognizers: each starts in `.possible` and, when it recognizes, moves to `.began`, then `.changed` repeatedly, then `.ended` or `.cancelled`. By default no two recognizers recognize simultaneously; the delegate's `gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)` defaults to `false`, and the documentation describes it as being asked when one recognizer's recognition would block the other. So the first pan to begin blocks the other, and nothing about that race looks at direction. ## The tools UIKit gives you | Tool | Where | Effect | |---|---|---| | `gestureRecognizerShouldBegin(_:)` | the recognizer's delegate, or a `UIView` override | asked when leaving `.possible`; `false` moves it to `.failed` | | `require(toFail:)` | on the recognizer that should wait | it stays in `.possible` until the other fails, and fails if the other begins or recognizes | | `gestureRecognizer(_:shouldRequireFailureOf:)` and `gestureRecognizer(_:shouldBeRequiredToFailBy:)` | delegate | the same dependency, decided per attempt, across hierarchies | | `gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)` | delegate | `true` lets both run at once | ## The usual fix 1. **Decide direction early.** Make the card pan's delegate implement `gestureRecognizerShouldBegin(_:)` and return `true` only when `velocity(in:)` shows a mostly horizontal drag. A vertical drag makes the card pan fail at once. 2. **Make the scroll view wait.** Call `scrollView.panGestureRecognizer.require(toFail: cardPan)`. If the card pan fails, the scroll view's pan carries on to its normal next state; if the card pan begins, the scroll view's pan fails. 3. **Handle every end state.** In the action method, treat `.ended` and `.cancelled` alike, snapping the card back or completing the archive, because a pan can be cancelled, for instance when its `isEnabled` is switched off mid-gesture. The cost of `require(toFail:)` is latency: for a drag that starts on a card, scrolling cannot begin until the card's pan has failed. Because step 1 settles the card pan on its first movement, the delay is small, but a failure requirement on a recognizer that decides late, such as a double tap, makes the waiting one feel sluggish. ## Choosing the right dependency API - `require(toFail:)` is fine when you create both recognizers and the set does not change. Apple's documentation points to the delegate methods when recognizers come from elsewhere in the app or a framework, or must be linked lazily across view hierarchies. - `gestureRecognizer(_:shouldRequireFailureOf:)` is called once per attempt to recognize, so the answer can depend on the current state. - Returning `false` from the failure-requirement or simultaneity delegate methods does not guarantee the opposite, because the other recognizer's own delegate or subclass may still set up the relationship. Returning `true` is guaranteed. ## When simultaneous recognition is right `true` from `gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)` suits gestures meant to combine, such as a pinch and a rotation on the same photo. For a card inside a list it means a diagonal drag both slides the card and scrolls the list, which reads as a glitch. Use it here only when that is the design. ## Variations - **A whole horizontal strip of cards.** A horizontally scrolling `UIScrollView` nested in a vertical one gives you a second scroll view's pan instead of a custom one; the same reasoning about which recognizer should wait applies. - **Controls inside the card.** A scroll view's `touchesShouldCancel(in:)` returns `true` by default unless the view is a `UIControl`, which is why a drag that starts on a button may not scroll. Override it in a scroll view subclass if drags from buttons should scroll. - **A screen-edge pan.** Apple's delegate documentation uses a `UIScreenEdgePanGestureRecognizer` as its example of a dynamic failure requirement: the other recognizers in that view's subtree are made to require the edge pan to fail, so they never start and then get cancelled mid-gesture. - **Debugging.** Set each recognizer's `name` and log state changes in the action methods to see which one began and which one failed.
- What exactly happens to the scroll view's pan after require(toFail:) when the card's pan begins?The dependency delays the scroll view's pan leaving `.possible`. If the card's pan transitions to `.began` or recognized, the scroll view's pan transitions to `.failed` and the list does not scroll for that touch sequence. If the card's pan fails, the scroll view's pan moves to its normal next state.
- Why might returning false from gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) not stop both recognizers running together?Each recognizer's delegate can be asked, and `true` from either is guaranteed to allow simultaneous recognition. Apple's documentation states that `false` is not a guarantee, because the other recognizer's delegate may return `true`. Check both delegates, including ones set by a framework.
- When do you use gestureRecognizer(_:shouldRequireFailureOf:) instead of require(toFail:)?When the recognizers are created in different places, such as a framework's recognizer you do not own, or when the requirement depends on state at the moment of recognition. The delegate method is called once per attempt and can link recognizers across view hierarchies, whereas `require(toFail:)` fixes the relationship once when both objects exist.
- Two recognizers on nested views can both recognize by default.
- require(toFail:) makes both pans run together once one has failed.
- Returning true from shouldRecognizeSimultaneouslyWith is the standard fix for a card in a list.
- Returning false from gestureRecognizerShouldBegin(_:) leaves the recognizer waiting in possible.
- A pan's action only needs to handle the ended state.