dulongj dulongj - 1 month ago 15
iOS Question

Change view controller after splash animation finishes

I'm trying to run a splash animation over a black screen when the app is first launched, and then transition into the initial view controller. To do so, I set the launchscreen.storyboard view controller to black, and then in didFinishLaunchingWithOptions I created and ran the animation as a subview (shown below) in the appDelegate. Currently the app launches on a black screen, the animation fades in, sleeps, and fades out correctly, but then stays on the black screen even though the initial view controller is set to a blank white background. I have tried multiple completion blocks for sending the app into the next view controller such as deleting the subview or trying to force it using rootViewController again but with no such luck. I'm fairly certain it's just one line of code I need but I can't seem to figure it out.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

self.window = UIWindow(frame: UIScreen.main.bounds)
let imageView = UIImageView(frame: self.window!.frame)
imageView.loadGif(name: "splashanimation")
imageView.alpha = 0.0
self.window!.addSubview(imageView)
let emptyView = UIViewController()
self.window?.rootViewController = emptyView
self.window!.makeKeyAndVisible()
UIView.animate(withDuration: 1.0, animations: {imageView.alpha = 1}, completion: { (value: Bool) in UIView.animate(withDuration: 1.0, animations: {sleep(UInt32(1.5))}, completion: { (value: Bool) in UIView.animate(withDuration: 2.0, animations: {imageView.alpha = 0}, completion: {

** SOMEHOW OPEN THE FIRST VIEW CONTROLLER **


})})})
UIApplication.shared.isStatusBarHidden = true
return true
}

Answer
let storyboard = UIStoryboard(name: "Main", bundle: nil)

let initialViewController = storyboard.instantiateViewControllerWithIdentifier("HomeView") as UIViewController

self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()

This sets an instance of the storyboard and then as you did with your launchscreen, just set which VC to be the initial.