为什么我在用户登录时看不到我的动画? [英] Why i don't see my animation when user is logged in?

查看:29
本文介绍了为什么我在用户登录时看不到我的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义 LaunchSreen,它在用户未登录时运行良好,但如果他已登录,我们将看不到动画(视图会直接进入主视图,不会等待动画完成).

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).

你知道为什么吗?

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)
    }
}

推荐答案

在这种情况下,解决方案是使用 DispatchQueue 延迟,例如

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 !!
        }
    }

这篇关于为什么我在用户登录时看不到我的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆