NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

How to create gesture chains using sequenced(before:)

Paul Hudson    @twostraws   

Updated for Xcode 12.5

SwiftUI lets us create new gestures out of sequences of other gestures, which allows us to trigger an action only when two gestures occur back to back – for example if the user drags a view then long-presses on it.

Because the sequenced views need to be able to reference each other, you can’t really create them as properties of your view. Instead, create them directly inside your body property, then use firstGesture.sequenced(before: secondGesture) to chain the two together into a single gesture.

As an example, this next code requires you to long press on the text view before dragging it:

struct ContentView: View {
    @State private var message = "Long press then drag"

    var body: some View {
        let longPress = LongPressGesture()
            .onEnded { _ in
                message = "Now drag me"

        let drag = DragGesture()
            .onEnded { _ in
                message = "Success!"

        let combined = longPress.sequenced(before: drag)


As you can see, I’ve made the text view update as the two gestures happen, so if you try it out you’ll be able to follow the progress of the gesture sequence.

Hacking with Swift is sponsored by Emerge

SPONSORED Emerge helps iOS devs write better, smaller apps by profiling binary size on each pull request and surfacing insights and suggestions. Companies using Emerge have reduced the size of their apps by up to 50% in just the first day. Built by a team with years of experience reducing app size at Airbnb.

Set up a demo!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.