【发布时间】:2023-03-21 15:50:02
【问题描述】:
我注意到,当我为背景着色时,删除视图时不会得到动画。
如果我删除Color(.orange).edgesIgnoringSafeArea(.all),那么隐藏动画将起作用,否则Modal 将突然消失。有什么解决办法吗?
struct ContentView: View {
@State var show = false
func toggle() {
withAnimation {
show = true
}
}
var body: some View {
ZStack {
Color(.orange).edgesIgnoringSafeArea(.all)
Button(action: toggle) {
Text("Modal")
}
if show {
Modal(show: $show)
}
}
}
}
struct Modal: View {
@Binding var show: Bool
func toggle() {
withAnimation {
show = false
}
}
var body: some View {
ZStack {
Color(.systemGray4).edgesIgnoringSafeArea(.all)
Button(action: toggle) {
Text("Close")
}
}
}
}
【问题讨论】:
标签: swiftui