In SwiftUI, how do simultaneously(with:), sequenced(before:) and exclusively(before:) differ when you combine two gestures on one view?
answer
- three ways to combine
- both at once, in order, or one
- precedence goes to the first
- the combined value's shape
basics
~20 ssimultaneously(with:) recognises both gestures at once; sequenced(before:) needs the first to succeed before the second starts; exclusively(before:) lets only one succeed, preferring the first, which is how a double tap and a single tap share a view.
solid answer
~40 sAll three are methods on `Gesture` that return one combined gesture. `simultaneously(with:)` evaluates both children at the same time; its value holds an optional for each. `sequenced(before:)` makes the second gesture wait until the first succeeds: Apple's example is a `LongPressGesture` sequenced before a `DragGesture`, so a card moves only after a press, and the value is an enum, `.first(...)` while the first runs and `.second(firstValue, secondValue?)` afterwards. `exclusively(before:)` lets only one child succeed and gives precedence to the first. For a photo that resets zoom on double tap and toggles its toolbar on single tap, write `TapGesture(count: 2).exclusively(before: TapGesture())`. Put the single tap first and it wins on the first touch, so the double tap never succeeds.
code
swift · 19 linesimport SwiftUI
struct PhotoPage: View {
@State private var zoom: CGFloat = 2
@State private var chromeHidden = false
var body: some View {
Image(systemName: "photo")
.resizable()
.scaledToFit()
.scaleEffect(zoom)
.gesture(
TapGesture(count: 2)
.onEnded { zoom = 1 } // double tap: reset
.exclusively(before:
TapGesture().onEnded { chromeHidden.toggle() }) // single tap
)
}
}go deeper
Recall the three composition methods and what each one means: both at once, one after the other, or only one.
Explain each combined value's shape and why the double tap goes first in an exclusive pair, with the single tap's delay as a consequence.
Model a sequenced gesture's phases as a @GestureState enum and keep composition separate from parent-versus-child attachment modifiers.
Keep composed gestures few and documented in shared components; every extra composed gesture adds latency and conflict risk to a screen.
## Three ways to combine two gestures When a view needs more than one gesture, you decide how they relate. SwiftUI's `Gesture` protocol has three composition methods, each returning a new gesture you attach with a single `gesture(_:)` call: | Method | Result type | Recognised when | Value shape | |---|---|---|---| | `simultaneously(with:)` | `SimultaneousGesture` | both children are evaluated at the same time | struct with optional `first` and `second` | | `sequenced(before:)` | `SequenceGesture` | the first succeeds, then the second | enum: `.first(First.Value)`, `.second(First.Value, Second.Value?)` | | `exclusively(before:)` | `ExclusiveGesture` | only one child succeeds; the first has precedence | enum: `.first(First.Value)`, `.second(Second.Value)` | Apple's article on composing gestures summarises the rule: simultaneous composition recognises both patterns together, sequenced composition recognises each subgesture in order, and exclusive composition recognises the whole pattern when only one subgesture is recognised. ## Exclusive: a double tap and a single tap on one photo A photo viewer often wants a **double tap to reset zoom** and a **single tap to show or hide the toolbar**. Two plain tap modifiers compete for the first tap. Exclusive composition states the intent: ```swift TapGesture(count: 2) .onEnded { zoom = 1 } .exclusively(before: TapGesture().onEnded { chromeHidden.toggle() }) ``` - `ExclusiveGesture` gives **precedence to its first gesture**, so the double tap is put first. - Reverse the order and the single tap succeeds on the very first touch; the double tap never gets its chance. - Because the double tap has precedence, the single tap can succeed only once the double tap can no longer match, so expect the single-tap action to arrive a moment after the finger lifts. ## Sequenced: a long press, then a drag Apple's own example sequences a `DragGesture` after a `LongPressGesture`, so a card can be moved only after the user has pressed on it. The combined value is an enum you switch over in `updating`: 1. `.first(true)`: the long press is in progress; show a pressed state. 2. `.second(true, nil)`: the long press succeeded and dragging may begin, but the drag has no value yet. 3. `.second(true, let drag?)`: the user is dragging; read `drag.translation`. 4. Anything else: inactive, which a `@GestureState` gets for free when it resets. The article models these phases with a small enum (`inactive`, `pressing`, `dragging(translation:)`) held in `@GestureState`, which keeps the view code readable. To make the move stick, its `onEnded` uses `guard case .second(true, let drag?) = value else { return }` and adds `drag.translation` to an offset kept in `@State`; a long press that ends without a drag matches nothing and changes nothing. While the user drags, the view shows the committed offset plus the live translation, the same two-layer pattern as any pan. ## Simultaneous: two gestures that both apply Simultaneous composition fits gestures that naturally happen together, such as a pinch and a two-finger rotation on a photo. Each optional in the value is `nil` while that child has produced nothing, so read both with defaults. The same applies in `onEnded`: a pinch that ends before the rotation has produced any value can reach `onEnded` with `second` still `nil`, so commit each part only when its value is there. ## Composition versus the attachment modifiers The three methods above build **one gesture on one view**. They are different from `simultaneousGesture(_:)` and `highPriorityGesture(_:)`, which are **view modifiers** deciding how a gesture attached to a view relates to gestures that the view and its subviews already define. Mixing the two ideas up is a frequent source of confusion: - Use a composition method when the two gestures belong to the same element and you want one combined value. - Use an attachment modifier when a parent's gesture must coexist with, or override, a child's. ## Mistakes that come up in review - **Listing the single tap first in an exclusive pair.** It always wins, so the double tap is dead code. - **Using simultaneous composition for single and double tap.** Both succeed on a double tap, so the toolbar toggles every time the user resets zoom. - **Sequencing a tap before a double tap.** `TapGesture().sequenced(before: TapGesture(count: 2))` asks for a tap followed by a separate double tap: three taps in all. - **Treating `.first(true)` as "the press succeeded".** It is reported while the long press is in progress; success shows up as `.second(true, ...)`.
- Why does the single-tap action feel slightly delayed once you add the double tap?With `TapGesture(count: 2).exclusively(before: TapGesture())`, the double tap has precedence, so the single tap can only win once the double tap can no longer match. SwiftUI has to wait out the window for a second tap first, so the single-tap action lands a moment later than it would on its own.
- In a long-press-then-drag gesture, how do you show that dragging may begin before the finger has moved?Match `.second(true, nil)` on the `SequenceGesture` value: the long press has succeeded and the drag has produced no value yet. Map it to a state such as "ready to drag" in a `@GestureState` enum and draw a border or lift effect from it.
- How does simultaneously(with:) differ from the simultaneousGesture(_:) modifier?`simultaneously(with:)` builds one gesture out of two and reports one combined value, attached to one view. `simultaneousGesture(_:)` attaches a separate gesture to a view so it is processed alongside the gestures that view and its subviews already define, such as a parent's tap that runs together with a child's.
- Gesture order inside exclusively(before:) does not matter
- simultaneously(with:) is the right way to pair a single tap with a double tap
- .first(true) in a sequenced long press means the press has already succeeded
- exclusively(before:) and highPriorityGesture(_:) are two names for the same thing