我正在尝试在首次启动应用时在黑屏上运行启动动画,然后转换到初始视图控制器。为此,我将launchscreen.storyboard视图控制器设置为黑色,然后在我创建的didFinishLaunchingWithOptions中将动画作为子视图(如下所示)在appDelegate中运行。目前,应用程序在黑屏上启动,动画淡入,睡眠和淡出,但即使初始视图控制器设置为空白背景,也会停留在黑屏上。我已经尝试了多个完成块来将应用程序发送到下一个视图控制器,例如删除子视图或尝试再次使用rootViewController强制它但没有这样的运气。我很确定它只是我需要的一行代码,但我似乎无法弄明白。
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
}
答案 0 :(得分:1)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("HomeView") as UIViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
这会设置故事板的实例,然后就像使用launchscreen
一样,只需将哪个VC设置为初始值。