问题
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