我有一个主视图,在这个视图中,我有一个在GeometryReader中的小弹出菜单,如下所示:
if (self.show){
GeometryReader{_ in
Menu()
}.background(Color.black.opacity(0.65))
}行~.background(Color.black.opacity(0.65))~实质上是使背景(即视图中没有弹出视图的每个部分)有点暗。我想做这样的事情:
if (self.show){
GeometryReader{_ in
Menu()
}.background(Color.black.opacity(0.65))
.background(.onTapGesture{
print("asdf")
})
}但不支持这种语法。我有什么办法能做到这一点吗?本质上,我希望这样,当我单击GeometryReader外部时,我可以切换一个变量(在本例中,去掉弹出视图)。
我尝试在主视图上创建一个TapGesture识别器,但是由于GeometryReader是主视图的一部分,所以当我点击GeometryReader弹出视图本身时,它就消失了。
有什么方法可以完成与我上面写的代码类似的事情吗?
谢谢
发布于 2021-03-11 18:59:22
下面是一个例子。我使用三个tapGestures:
struct ContentView: View {
@State private var showMenu: Bool = false
var body: some View {
ZStack {
// The Main View.
Text("Tap Me!")
.padding()
.onTapGesture {
showMenu.toggle()
print("Tapped Main View")
}
// The Menu View (shown on top of the Main View).
if showMenu {
GeometryReader { _ in
Text("Menu")
.padding()
.onTapGesture {
// do something here
print("Tapped Menu")
}
}
// The Background View that darkens the whole screen.
.background(
Color.gray.opacity(0.2)
.edgesIgnoringSafeArea(.all)
)
.onTapGesture {
showMenu.toggle()
print("Tapped Background")
}
}
}
}
}敲一下“拍我!”(主视图)显示菜单视图。“菜单”捕捉点击动作-做任何你想做的事。
每当用户点击“菜单”之外的“菜单”时,背景上的tapGesture就会识别出“菜单”,并删除“菜单”,包括变暗的背景-->主视图再次变亮。
https://stackoverflow.com/questions/62604021
复制相似问题