UberJason
UberJason

Reputation: 3163

SwiftUI Form on watchOS: tap gesture intermittently fails

On watchOS, I'm trying to make a really simple form consisting of a Section, with a list of text values, which I can tap to perform some action. Code below.

struct ContentView: View {
    var body: some View {
        Form {
            Section(header: Text("Section 1")) {
                ForEach(0 ..< 3) { int in
                    Text("Hello \(int)")
                        .onTapGesture(count: 1) {
                            print("Tapped \(int)")
                        }
                }
            }
        }
    }
}

When I run this in the simulator, each cell tap only intermittently prints as desired. For any cell, sometimes a tap will emit a print, and sometimes nothing happens. (More often, nothing happens. Only on occasion does a print emit.)

Am I doing something wrong, or is this a bug in the Apple Watch simulator? Occurs in both the 40mm and 44mm simulators, as of Xcode 11 beta 5 on macOS Catalina beta 5.

Upvotes: 0

Views: 435

Answers (1)

UberJason
UberJason

Reputation: 3163

To answer my own question, I filed this with Apple Feedback and got a reply:

Please know that our engineering team has determined that this issue behaves as intended based on the information provided.

Text with an onTapGesture() modifier will trigger the closure when the text itself is tapped. In your example project, tapping on the text itself triggers the block, but tapping outside the text does not. If you want an action when the List row is tapped, place a Button in the List with Text as the content of the Button.

They also provided this sample code snippet:

List {
    Section(header: Text("Section 1")) {
        ForEach(0..<3) { value in
               Button(action: {
                    print("Tapped \(value)")
                }) {
                    Text("Hello \(value)")
                }
            }
         }
    }

No answer about the Form construct, but the List example makes sense, if slightly unintuitive at first glance.

Upvotes: 2

Related Questions