有人知道为什么以下不是动画并且不能完全工作以及如何解决这个问题吗?问题是我们的一些动画在这种情况下没有显示。也许有一种解决方法?我读了好几个小时的文章等等......什么也没找到
我最终想要实现的是:
SignUpLogin 屏幕出现
.move(edge: .top)
和 消失时都有动画.opacity
注销屏幕同时具有:
.blur
和.opacity
出现和消失时的动画。
当视图重新出现时,模糊和不透明度停止工作(但它们在消失时工作,参见 gif)。
import SwiftUI
import Foundation
import Combine
class ViewRouter: ObservableObject {
@Published var signUpLoginView_active: Bool = true
@Published var newUserView_active: Bool = false
}
struct ContentView: View {
@EnvironmentObject var viewRouter: ViewRouter
@Environment(\.colorScheme) var colorScheme
var body: some View {
ZStack {
if colorScheme == .dark {
Color.white.ignoresSafeArea()
} else {
Color.black.ignoresSafeArea()
}
if viewRouter.newUserView_active {
NewUserView()
.transition(.opacity)
.zIndex(4)
}
if viewRouter.signUpLoginView_active {
SignUpLoginView()
.transition(.move(edge: .top))
.zIndex(5)
}
}
.edgesIgnoringSafeArea(.all)
}
}
struct SignUpLoginView: View {
@EnvironmentObject var viewRouter: ViewRouter
@Environment(\.colorScheme) var colorScheme
var body: some View {
ZStack() {
colorScheme == .dark ? Color.black.ignoresSafeArea().zIndex(3) : Color.white.ignoresSafeArea().zIndex(3)
if colorScheme == .dark {
Color.white.ignoresSafeArea()
.opacity(viewRouter.signUpLoginView_active ? 0 : 1)
.zIndex(4)
} else {
Color.black.ignoresSafeArea()
.opacity(viewRouter.signUpLoginView_active ? 0 : 1)
.zIndex(4)
}
//
Button("Login") {
withAnimation(.easeInOut(duration: 3)) {
viewRouter.signUpLoginView_active.toggle()
viewRouter.newUserView_active.toggle()
}
}.zIndex(3)
}
}
}
struct NewUserView: View {
@EnvironmentObject var viewRouter: ViewRouter
@Environment(\.colorScheme) var colorScheme
var body: some View {
ZStack {
TabView {
Button("Logout") {
withAnimation(.easeInOut(duration: 3)) {
viewRouter.signUpLoginView_active.toggle()
viewRouter.newUserView_active.toggle()
}
}
.tabItem {
Image(systemName: "person.crop.circle.fill")
Text("My Profile")
}
}
}
.blur(radius: (viewRouter.signUpLoginView_active ? 3 : 0), opaque: true)
.padding(viewRouter.signUpLoginView_active ? -20 : 0)
}
}