Swiftui 快捷模式
我想通过点击我称之为模态的同一个按钮,或者点击屏幕上的任何地方来消除模态 但当弹出窗口弹出时,我无法与它们进行交互。你能给我一个解决方案吗Swiftui 快捷模式,swiftui,modal-dialog,dismiss,Swiftui,Modal Dialog,Dismiss,我想通过点击我称之为模态的同一个按钮,或者点击屏幕上的任何地方来消除模态 但当弹出窗口弹出时,我无法与它们进行交互。你能给我一个解决方案吗 ZStack{ Color.black.opacity(0.4) .edgesIgnoringSafeArea(.all) .animation(.none) VStack{
ZStack{
Color.black.opacity(0.4)
.edgesIgnoringSafeArea(.all)
.animation(.none)
VStack{
Circle().fill(Color.orange)
.frame(width: 70, height: 70)
.overlay(Image(systemName: "questionmark.circle").font(.system(size: 50)).foregroundColor(.white
))
.offset(y: 40)
.zIndex(1)
VStack{
Color.orange.frame(height: 40)
Spacer()
Text("Let the popup open")
Spacer()
Button("Present!") {
isPresented.toggle()
}
.fullScreenCover(isPresented: $isPresented){
FullScreenModalView(showPopUP: $showPopUp)
}.padding(.vertical, 12)
.frame(maxWidth: .infinity)
.background(Color.orange)
}.background(Color.white)
.cornerRadius(12)
}.frame(height: 250)
.background(Color.clear)
.padding(.horizontal, 35)
.offset(y: 150)
.scaleEffect(x: showPopUp ? 1 : 0.8, y: showPopUp ? 1 : 1.3)
.animation(.interactiveSpring(response: 0.3, dampingFraction: 0.3), value: animate)
}.opacity(showPopUp ? 1 : 0)
您的示例没有包含足够的代码进行编译,因此我制作了一个较小的、最少的示例来说明这一想法:
struct ContentView: View {
@State var modalPresented = false
var body: some View {
Button(action: {
modalPresented.toggle()
}) {
Text("Present")
}.fullScreenCover(isPresented: $modalPresented) {
ModalContent(presented: $modalPresented)
}
}
}
struct ModalContent : View {
@Binding var presented : Bool
var body: some View {
VStack {
Text("Modal content")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(
Color.green.edgesIgnoringSafeArea(.all)
.onTapGesture {
presented = false
}
)
//.contentShape(Rectangle())
//.onTapGesture { presented = false }
}
}
根据定义,由于您使用的是全屏覆盖,您将无法与原始按钮交互——它将被覆盖
在我的示例中,我使用模态的背景来响应点击手势以关闭模态,但我也注释了几行,您可以在主堆栈上使用
contentShape
,并在那里捕获点击手势。我想我问得不够清楚。我的意思完全是关于弹出,我不知道那是什么意思。