1

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

Flincorp
  • 751
  • 9
  • 22
  • Because Home is at the top of your conditional you only see the splash when none of the conditions meet. If you want to run your splash no matter what take it out of the conditional and/or put it at the top. – lorem ipsum Dec 14 '20 at 21:51
  • Hummm it's not working and the animation is broken with your suggestion... – Flincorp Dec 15 '20 at 00:09

1 Answers1

2

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 !!
        }
    }
Asperi
  • 228,894
  • 20
  • 464
  • 690