SwiftUI无隐藏动画
我注意到,当我给背景着色时,在删除视图时,我不会得到动画 如果我删除SwiftUI无隐藏动画,swiftui,Swiftui,我注意到,当我给背景着色时,在删除视图时,我不会得到动画 如果我删除Color(.orange).edgesIgnoringSafeArea(.all)则隐藏动画将起作用,否则Modal将突然消失。有什么解决办法吗 struct ContentView: View { @State var show = false func toggle() { withAnimation { show = true }
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")
}
}
}
}
您需要制作包含移除视图的可设置动画的容器(这使得可以将动画保留在一个位置)。这是可能的解决办法 使用Xcode 12/iOS 14进行测试
struct ContentView: View {
@State var show = false
func toggle() {
show = true // animation not requried
}
var body: some View {
ZStack {
Color(.orange).edgesIgnoringSafeArea(.all)
Button(action: toggle) {
Text("Modal")
}
VStack { // << major changes
if show {
Modal(show: $show)
}
}.animation(.default) // << !!
}
}
}
struct Modal: View {
@Binding var show: Bool
func toggle() {
show = false // animation not requried
}
var body: some View {
ZStack {
Color(.systemGray4).edgesIgnoringSafeArea(.all)
Button(action: toggle) {
Text("Close")
}
}
}
}
struct ContentView:View{
@状态变量show=false
func toggle(){
show=true//不需要动画
}
var body:一些观点{
ZStack{
颜色(.橙色).边缘识别安全区域(.all)
按钮(操作:切换){
文本(“模态”)
}
VStack{//