转换不适用于 SwiftUI 中的路由器



我正在制作一个基于 SwiftUI 的应用程序。

我在主层次结构中嵌入了一个路由器来管理应用程序导航。一开始这工作得很好,但后来我添加了第二级路由器,转换被破坏并停止工作。

实际上,两个路由器中的导航都运行良好,但无法使其与过渡一起工作。

应用根目录转到此文件:

var body: some View {
VStack {
if authRouter.isFirstLaunch {
AuthView()
} else if sessionState.launchListeningCompleted || sessionState.user != nil || sessionState.error != nil {
Home()
} else {
ZStack {
Image("Icon")
VStack {
Spacer()
ActivityIndicatorUI(animating: .constant(true), style: .medium)
.padding(.bottom)
Image("Launcher")
.padding(.bottom, 95.0)
.padding(.top)
}
}
}
}.onAppear(perform: isFirstLaunch)
}

从本质上讲,如果是第一次启动应用程序,它将转到AuthView文件,该文件是这样的路由器:

struct AuthView: View {
@EnvironmentObject var authRouter: AuthRouter
var body: some View {
VStack {
if authRouter.viewName == AuthViews.Onboarding {
Onboarding()
} else if authRouter.viewName == AuthViews.SignIn {
SignIn()
.transition(.scale)
} else if authRouter.viewName == AuthViews.SignUp {
SignUp()
.transition(.move(edge: .trailing))
} else if authRouter.viewName == AuthViews.ForgotPassword {
ForgotPassword()
.transition(.move(edge: .trailing))
}
}
}

}

这些转换不起作用。一开始,这是我添加的第一个路由器,它正在工作。然后在第二级它停止工作。

感谢您的帮助!

将动画添加到父容器

var body: some View {
VStack {
if authRouter.viewName == AuthViews.Onboarding {
Onboarding()
} else if authRouter.viewName == AuthViews.SignIn {
SignIn()
.transition(.scale)
} else if authRouter.viewName == AuthViews.SignUp {
SignUp()
.transition(.move(edge: .trailing))
} else if authRouter.viewName == AuthViews.ForgotPassword {
ForgotPassword()
.transition(.move(edge: .trailing))
}
}.animation(.default)     // << here !! (any animation you like)
}

并确保authRouter.viewName始终在DispatchQueue.main上更新。

最新更新