Why i don't see my animation when user is logged in?

ⅰ亾dé卋堺 提交于 2021-01-05 07:08:55

问题


I created a custom LaunchSreen which works well when the user is not logged in but if he is logged in we do not see the animation (the view goes right to the Home view and doesn't wait for the animation to be completed).

Do you have any idea why?

import SwiftUI

struct LaunchScreen: View {
    @EnvironmentObject var session: SessionStore
    @State private var animationDone = false
    @State private var rotation = 0.0
    
    func getUser () {
        session.listen()
    }
    
    var body: some View {
        Group{
            if (session.session != nil && animationDone) {
                Home()
            }
            else if (session.session == nil && animationDone) {
                Login()
            }
            else {
                ZStack {
                    Color(#colorLiteral(red: 0.259467423, green: 0.5342320204, blue: 0.7349982858, alpha: 1))
                    VStack {
                        HStack (alignment: .center, spacing: 0, content: {
                            
                            Text("Se")
                                .foregroundColor(.white)
                                .font(.system(size: 40))
                            Text("e")
                                .foregroundColor(.white)
                                .font(.system(size: 40))
                                .rotation3DEffect(Angle(degrees: rotation), axis: (x: 0, y: 1, z: 0))
                        })
                    }
                }.edgesIgnoringSafeArea(.all)
            }
        }
        .onAppear{
            withAnimation(Animation.easeInOut(duration: 1)){
                rotation += 180
            }
            withAnimation(Animation.linear.delay(1.5)){
                animationDone = true
            }
        }
        .onAppear(perform: getUser)
    }
}


回答1:


In this scenario the solution is to delay with DispatchQueue, like

    }
    .animation(.linear, value: animationDone)             // << this !!
    .onAppear{
        withAnimation(Animation.easeInOut(duration: 1)){
            rotation += 180
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            animationDone = true                         // << and this !!
        }
    }


来源:https://stackoverflow.com/questions/65296618/why-i-dont-see-my-animation-when-user-is-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!