How to resolve SwiftUI Tap Conflict?

Viewed 508

When I use SwiftUI to make my view,I can't make the tap event work. My Code like:

                    VStack{
                        HStack{
                            //SomeView...
                        }
                        .onTapGesture {
                            print("HStack Tap Work")
                        }
                    }
                    .onTapGesture {
                        print("VStack Tap Work")
                    }

And I just want to print "VStack Tap Work" when i tap the other area of VStack expect the HStack,and print "HStack Tap Work" when i tap the HStack.

But it always print "VStack Tap Work".So how can i resolve it?

1 Answers

The handling order might depend on real views hierarchy (because stacks usually tight around content), so try to separate gestures by priority, like

HStack {
    //SomeView...
}.highPriorityGesture(TapGesture().onEnded { // high priority !!
    print("HStack Tap Works")
})
Related