SwiftUI: Cancel TapGesture on parent view


I have view hierarchy in SwiftUI like

ParentView { //other views ChildView().highPriorityGesture(TapGesture().onEnded { print("Tap!") }) // other views }self.gesture(tap)

And I want to have parent view handle all taps on the screen in spite of cases when user taps onto ChildView. Now both closures executes. How to stop tap gesture events propagating up view hierarchy?


Well, probably there is some specific in which exactly ChildView and ParentView, because as tested below (Xcode 11.2 / iOS 13.2) child view gesture just overrides parent view gesture.

Here is demo.. tapped in yellow area, then tapped in green area - no mix callbacks

Complete module code

import SwiftUI struct TestGesturesPriority: View { var body: some View { VStack { Text("Hello, World!") .padding() .background(Color.yellow) .gesture(TapGesture().onEnded { print(" -- child") }) } .frame(width: 400, height: 400) .background(Color.green) .gesture(TapGesture().onEnded { print(">> parent") }) } }

Update: variant for List-Row

Yeees... <em>List (Parent) - Row (Child)</em> case appeared very challenging... please find below approach, it looks weird but tested & works

struct TestGesturesPriority: View { let parentGesture = TapGesture().onEnded { // just for convenience print(">> parent") } @GestureState private var captured = false var body: some View { List { Text("Hello, World!").padding() .background(Color.yellow) .allowsHitTesting(true) .gesture(DragGesture(minimumDistance: 0) // mimic Tap .updating($captured, body: { (value, state, transaction) in state = true // mark captured (will be reset automatically) }) .onEnded { value in // like Tap, but can be ignored if delta // is large or out of view print(" -- child") } ) } .gesture(parentGesture, including: captured ? .subviews : .gesture) } }

To summarise - actually I think it is another List defect


There's a slightly cleaner way to solve the tap localisation issue in List as follows:

struct TestListGestures: View { var body: some View { List { Text("Hello, World!").padding() .background(Color.yellow) .gesture(LongPressGesture(minimumDuration: 0.001) // resolve response time .onEnded { value in print(" -- child") } ) } .gesture(LongPressGesture(minimumDuration: 0.001).onEnded({ _ in print(" -- parent") }), including: .gesture) } }



  • SwiftUI: Cancel TapGesture on parent view
  • SwiftUI: Cancel TapGesture on parent view
  • value error for simple definition to generate list of colors
  • mysql concat string with result [closed]
  • How can I fill the space between a left and right float without making the right float wrap?
  • How to enable GZIP for RestController? [duplicate]
  • How to properly handle datetime comparisons in an entire DataFrame with NaT values?
  • Calculate number of days between two dates inside Django templates
  • Javascript increment operation order of evaluation
  • file_get_contents Adding numbers from 2 text files
  • Tab bar hidden by view controller when pushed inside my UITabBarController
  • Oracle MERGE: only NOT MATCHED is triggered
  • Invalid date [dd/mm/yyyy] in safari? How to display in dd/mm/yyyy format using jquery
  • Azure deployment virtual directory [duplicate]
  • starting headless firefox with Xvfb does not work
  • DB2 Unique Constraint over multiple Columns
  • Dynamically create controls in android using Kotlin
  • Mysql setting default value to be items auto increment key
  • Optimization of Neural Network input data
  • Simulate onFling() programmatically instead of detecting it (Android)
  • How do I set context and followup event in one intent?
  • ClearCase can I use clearexport_ccase/clearimport to copy VOB data to a VOB on a different machine
  • Implementing and using MinMax with four in row (connect4) game
  • How to intercept local server web requests using Burp in Internet Explorer
  • Web scraping pdf files from HTML
  • Eric5: The OK button of 'new project' dialog is disable
  • How to create mirrored image effect with CSS single element
  • Protractor Page objects - TypeError: Object # has no method 'methodName'
  • Shiny - change the size (padding?) of dropdown menu (select tags) smaller
  • jQuery scrollTop if URL has hash