How to know when the app launch screen is gone in SwiftUI?

167 Views Asked by At

I mimic the launch screen in a SwiftUI view. In the launch screen I have a logo. Once the launch screen is gone it needs to move to a different position. However now when I launch the app the animation begins earlier than the launch screen that is fading out. Giving a ghosting effect. I like this to be one smooth transition. In order to do this, I like to know when the launch screen is completely vanished so I can start the animation, but how?

1

There are 1 best solutions below

0
Mark On

Since I haven't received an answer I presume it does not exist. So my 'close enough' solution is based on the following:

  1. Wait for the scene to become active. It will be when the app finished the animation from app icon to full screen on iOS.
  2. Wait for the launch screen fading out animation to complete.
struct MainView: View {
    var body: some View {
        Text("Hello World")
            .onFinishLaunching {
                // Do stuff
            }
    }
}

struct FinishLaunchingViewModifier: ViewModifier {
    @Environment(\.scenePhase) var scenePhase
    
    @State private var didFinishLaunching: Bool = false
    
    let perform: () -> Void
    
    func body(content: Content) -> some View {
        content
            .onChange(of: scenePhase) { newPhase in
                guard newPhase == .active, didFinishLaunching == false else { return }
                didFinishLaunching = true
            }
            .onChange(of: didFinishLaunching) { _ in
                guard didFinishLaunching else { return }
                perform()
            }
    }
}

extension View {
    func onFinishLaunching(perform: @escaping () -> Void) -> some View {
        modifier(FinishLaunchingViewModifier(perform: perform))
    }
}