为什么我在用户登录时看不到我的动画?
Why i don't see my animation when user is logged in?
我创建了一个自定义的 LaunchSreen,它在用户未登录时运行良好,但如果他已登录,我们就看不到动画(视图直接转到主页视图,不等待动画显示完成)。
你知道为什么吗?
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
延迟,例如
}
.animation(.linear, value: animationDone) // << this !!
.onAppear{
withAnimation(Animation.easeInOut(duration: 1)){
rotation += 180
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
animationDone = true // << and this !!
}
}
我创建了一个自定义的 LaunchSreen,它在用户未登录时运行良好,但如果他已登录,我们就看不到动画(视图直接转到主页视图,不等待动画显示完成)。
你知道为什么吗?
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
延迟,例如
}
.animation(.linear, value: animationDone) // << this !!
.onAppear{
withAnimation(Animation.easeInOut(duration: 1)){
rotation += 180
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
animationDone = true // << and this !!
}
}