本文介绍了SwiftUI:在父视图上取消TapGesture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SwiftUI中具有视图层次结构,如

I have view hierarchy in SwiftUI like

ParentView {
//other views

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

我希望让父视图能够处理屏幕上的所有轻按,即使用户轻按ChildView时也是如此.现在,两个闭包都将执行.如何停止点击手势事件传播视图层次?

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?

推荐答案

List中,有一种更简洁的方法可以解决抽头本地化问题,如下所示:

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:在父视图上取消TapGesture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:20