Skip to main content

How to make two gestures recognize at the same time using simultaneousGesture()

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to make two gestures recognize at the same time using simultaneousGesture() 관련

SwiftUI by Example

Back to Home

How to make two gestures recognize at the same time using simultaneousGesture() | SwiftUI by Example

How to make two gestures recognize at the same time using simultaneousGesture()

Updated for Xcode 15

By default SwiftUI will trigger only one gesture recognizer action at a time, which is usually whichever one is the front-most view in your hierarchy – it would prefer a recognizer on a child view rather than its parent, for example. If you want to override this behavior to make two gestures trigger at once, you should use the simultaneousGesture() when creating your second and subsequent gestures.

For example, in this code we have two tap gestures, but SwiftUI will execute only the one attached to the circle because it's the child of the VStack:

struct ContentView: View {
    var body: some View {
        VStack {
            Circle()
                .fill(.red)
                .frame(width: 200, height: 200)
                .onTapGesture {
                    print("Circle tapped")
                }
        }
        .onTapGesture {
            print("VStack tapped")
        }
    }
}

Download this as an Xcode projectopen in new window

If you want both gestures to trigger – i.e., if you want both “Circle tapped” and “VStack tapped” to be printed – you should use simultaneousGesture() on the VStack like this:

struct ContentView: View {
    var body: some View {
        VStack {
            Circle()
                .fill(.red)
                .frame(width: 200, height: 200)
                .onTapGesture {
                    print("Circle tapped")
                }
        }
        .simultaneousGesture(
            TapGesture()
                .onEnded { _ in
                    print("VStack tapped")
                }
        )
    }
}

Download this as an Xcode projectopen in new window

Note

You should use simultaneousGesture() with the gesture that would otherwise not be executed otherwise it won't work. So, in our previous example using simultaneousGesture() with the circle and a simple onTapGesture() with the VStack will still print just “Circle tapped” – it won't do what you expect.

Similar solutions…
How to make two views the same width or height | SwiftUI by Example

How to make two views the same width or height
How to fill and stroke shapes at the same time | SwiftUI by Example

How to fill and stroke shapes at the same time
How to force one gesture to recognize before another using highPriorityGesture() | SwiftUI by Example

How to force one gesture to recognize before another using highPriorityGesture()
How to stop system gestures from interfering with your own | SwiftUI by Example

How to stop system gestures from interfering with your own
How to detect shake gestures | SwiftUI by Example

How to detect shake gestures

이찬희 (MarkiiimarK)
Never Stop Learning.