watchOS 上的 SwiftUI 表单:点按手势间歇性失败
SwiftUI Form on watchOS: tap gesture intermittently fails
在 watchOS 上,我试图制作一个非常简单的表单,其中包含一个部分和一个文本值列表,我可以点击它来执行一些操作。下面的代码。
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)")
}
}
}
}
}
}
当我在模拟器中 运行 执行此操作时,每个单元格点击只会根据需要间歇性地打印。对于任何单元格,有时点击会发出打印,有时什么也不会发生。 (更常见的是,什么也没有发生。只有偶尔打印出来。)
我是不是做错了什么,或者这是 Apple Watch 模拟器中的错误?从 macOS Catalina beta 5 上的 Xcode 11 beta 5 开始,在 40mm 和 44mm 模拟器中都会出现。
为了回答我自己的问题,我在 Apple 反馈中提交了这个问题并得到了回复:
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.
他们还提供了这个示例代码片段:
List {
Section(header: Text("Section 1")) {
ForEach(0..<3) { value in
Button(action: {
print("Tapped \(value)")
}) {
Text("Hello \(value)")
}
}
}
}
没有关于 Form 构造的答案,但 List 示例是有道理的,虽然乍一看有点不直观。
在 watchOS 上,我试图制作一个非常简单的表单,其中包含一个部分和一个文本值列表,我可以点击它来执行一些操作。下面的代码。
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)")
}
}
}
}
}
}
当我在模拟器中 运行 执行此操作时,每个单元格点击只会根据需要间歇性地打印。对于任何单元格,有时点击会发出打印,有时什么也不会发生。 (更常见的是,什么也没有发生。只有偶尔打印出来。)
我是不是做错了什么,或者这是 Apple Watch 模拟器中的错误?从 macOS Catalina beta 5 上的 Xcode 11 beta 5 开始,在 40mm 和 44mm 模拟器中都会出现。
为了回答我自己的问题,我在 Apple 反馈中提交了这个问题并得到了回复:
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.
他们还提供了这个示例代码片段:
List {
Section(header: Text("Section 1")) {
ForEach(0..<3) { value in
Button(action: {
print("Tapped \(value)")
}) {
Text("Hello \(value)")
}
}
}
}
没有关于 Form 构造的答案,但 List 示例是有道理的,虽然乍一看有点不直观。