我正在创建一个工具提示系统。
如果用户触摸工具提示之外的任何地方,我想关闭该工具提示。
我希望工具提示外部的触摸既可以关闭工具提示,又可以激活用户点击的任何控件。 (因此,您可以打开工具提示,然后仍然单击工具提示外部的按钮,并在第一次点击时将其激活。)
为此,我有一个不可见的视图来处理轻击手势并关闭工具提示,但是我不知道如何使SwiftUI不会拦截并取消轻击手势。在网络上,这相当于不调用event.stopPropagation()
和event.preventDefault()
或calling super in touchesBegan:
in UIKit。
有什么想法吗?
答案 0 :(得分:2)
您需要使用此修饰符:
.allowsHitTesting(false)
答案 1 :(得分:0)
这里是可能方法的演示。使用Xcode 11.4 / iOS 13.4进行了测试
struct ContentView: View {
var body: some View {
VStack {
Button("Button") { print("> button tapped")}
}
.frame(width: 200, height: 200)
.contentShape(Rectangle()) // makes all area tappable
.simultaneousGesture(TapGesture().onEnded({
print(">>> tooltip area here")
}))
.border(Color.red) // just for demo show area
}
}