在具有具有 onTapGestures 的子视图的 View 上调用 onTapGesture 方法,无需重写

Wis*_*Eye 5 swiftui

我希望能够将 a 添加onTapGesture{}到视图中,而不覆盖onTapGesture{}可能已经属于该视图一部分的 s 。

例子:

struct NotAPieceOfLettuce: View {
    var body: some View{
      Text("what am I?").onTapGesture{print("I am not a piece of lettuce")}
     }
}

struct Banana: View {
    var body: some View{
       NotAPieceOfLettuce().onTapGesture{print("just a banana, not lettuce")}
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码实际上会just a banana, not lettuce在点击时打印,尽管我希望它打印I am not a piece of lettuce并且也just a banana, not lettuce......

我已经尝试过 .simultaneousGesture(),但程序输出与上面的示例代码没有什么不同。

感谢您的阅读,WiseEye YT。

Asp*_*eri 5

应在两侧指定同时手势,例如

struct NotAPieceOfLettuce: View {
    var body: some View{
      Text("what am I?")
        .simultaneousGesture(TapGesture().onEnded{print("I am not a piece of lettuce")})
     }
}

struct Banana: View {
    var body: some View{
       NotAPieceOfLettuce()
        .simultaneousGesture(TapGesture().onEnded{print("just a banana, not lettuce")})
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 Xcode 13.4 / iOS 15.5 进行测试

演示