对于UIKit和ViewControllers,我是一个noobie。我试图从SplashScreenViewController
切换到GameViewController
这是一个SKView。 GameViewController加载正常,因为我可以听到游戏音乐开始,但SplashScreenController
永远不会从屏幕上消失。所以我基本上SplashScreenController
留在屏幕上,我可以在后台听到GameViewController
。我做错了什么?
这是SplashScreenViewController
:
class SplashScreenViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 46/255, green: 83/255, blue: 160/255, alpha: 1.0)
let image : UIImage = UIImage(named:"splashScreen.png")!
let bgImage = UIImageView(image: image)
bgImage.frame = CGRect(origin: CGPoint(x: 3,y: -3), size: CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(bgImage)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "GameViewController")
self.present(controller, animated: true, completion: nil)
self.dismiss(animated: true, completion: nil)
}
deinit {
print("Object with name SplashScreenViewController is being released")
}
}
答案 0 :(得分:3)
您可以设置窗口的rootviewcontroller以将闪屏替换为游戏屏幕。 代码看起来像这样
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window!.rootViewController = gameViewController
答案 1 :(得分:2)
据我所知,您希望将SplashScreenViewController
替换为GameViewController
。您当前的代码在解除它之后以模态和直接方式呈现GameViewController
。因此没有显示新的视图控制器。
您的代码中的另一个问题是缺少时间差异,目前初始化启动屏幕,如果您的代码正常工作,它将被GameViewController
替换,因此您只需请参阅GameViewController
。
class SplashScreenViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 46/255, green: 83/255, blue: 160/255, alpha: 1.0)
let image : UIImage = UIImage(named:"splashScreen.png")!
let bgImage = UIImageView(image: image)
bgImage.frame = CGRect(origin: CGPoint(x: 3,y: -3), size: CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(bgImage)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "GameViewController")
// Here we create a dispatch queue to do some other code after an amount of time. In this case, one second.
let dispatchTime = DispatchTime.now() + .seconds(1)
DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
self.navigationController?.setViewControllers([controller!], animated: false)
}
}
}
此设置将在一秒钟后用GameViewController替换SplashScreenViewController。为了实现这一点,并且基本上在视图控制器之间进行所有导航,您必须将第一个视图控制器包装为UINavigationController
。