How to disable multiple TapGestures from being recognized simultaneously in SwiftUI

I'm trying to build a simple List in SwiftUI, each row will perform an action on tap, here's a sample code:

struct ContentView: View {
  var body: some View {
    List {
      ForEach(1..<10) { i in
        Text("Hello, world!")
          .onTapGesture {
            print("Tapped: ", i)
          }
      }
    }
  }
}

This works, however, if user multi-taps on two rows, the console is going to print twice. Not great if I'm pushing a secondary view controller. Is there a way to have some sort of exclusiveTouch property to each List item, similar to what is in UIKit?

New in iOS 16 is a contextMenu(forSelectionType:menu:primaryAction:) modifier which accepts a primaryAction closure that is run, for example, when the user taps on a list row.

You do, however, need to bind your list's selection value to a stored @State property.

Can can use it like this:

@State private var selectedNum: Int?

List(selection: $selectedNum) {
    ForEach(1..<10) { i in
        Text("Hello, world!")
    }
}
.contextMenu(forSelectionType: Int.self, menu: { _ in }) { nums in
    print("Tapped: \(nums.first)")
}

You could keep track with a State var:

struct ContentView: View {
    @State var tapped = false

    var body: some View {
        List {
            ForEach(1..<10) { i in
                Text("Hello, world! \(i)")
                    .onTapGesture {
                        if !tapped { print("Tapped: ", i) }
                        tapped = true
                    }
            }
        }
    }
}

This works for only one time tap, and stop any future taps. I need to only prevent simultaneous taps.

How to disable multiple TapGestures from being recognized simultaneously in SwiftUI
 
 
Q