过渡不适用于 SwiftUI 中的路由器

Elv*_*vin 2 iphone ios swift 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)
}
Run Code Online (Sandbox Code Playgroud)

本质上,如果是第一次启动应用程序,它将转到 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))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

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

感谢帮助!

Asp*_*eri 5

向父容器添加动画

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)
}
Run Code Online (Sandbox Code Playgroud)

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