I stumbled upon an event bubbling problem related to SwiftUI's ScrollView
and have been able to reduce it to a short snippet of code. Please see below:
struct ContentView: View {
var body: some View {
ScrollView() {
Rectangle()
.fill(Color.red)
.frame(width: 200, height: 200)
.onTapGesture {
print("Rectangle onTapGesture")
}
}
.onTapGesture {
print("ScrollView onTapGesture")
}
}
}
When tapping outside of the Rectangle, I see in the console:
ScrollView onTapGesture
However, when tapping the Rectangle, I see two lines being printed:
ScrollView onTapGesture
Rectangle onTapGesture
The ScrollView seems to be responding to its child's event as well... that's not supposed to happen, right? What can I do to stop this?
EDIT: just to add to the madness, these two lines don't always appear in the same order! I've seen them swapped when restarting the app without any changes to the code.
My goal was to use an onTapGesture on the ScrollView to catch "dismissing" taps i.e., taps that were not caught/handled by any of the ScrollView's children.
Thank you very much!