【问题标题】:How make a transition between several ViewControllers from SKScene?如何在 SKScene 的几个 ViewController 之间进行转换?
【发布时间】:2014-10-10 21:05:14
【问题描述】:

我正在寻找使用 SpriteKit 从 gameScene 更改我的 ViewController。 我有 3 个场景和 3 个不同的 ViewControllers。

当我的应用程序以 PresentationViewController 启动时(请参见屏幕截图 1),我从 GameScene (GameViewController) 到 GameOverViewController 的转换代码不起作用,我可以读取错误消息。

错误信息:

whose view is not in the window hierarchy!

我的代码是:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let settingController: UIViewController = storyboard.instantiateViewControllerWithIdentifier("PresentationViewController") as UIViewController
    let vc = self.view?.window?.rootViewController
    vc?.presentViewController(settingController, animated: true, completion: nil)

当我的应用以 GameViewController 启动时(参见屏幕截图 2),转换代码运行良好。

我不知道为什么这两种情况有区别。

PresentationViewController 和 GameViewController 之间的转换是使用带有此代码的 UIButton 的信息:

override func viewDidLoad() {

    super.viewDidLoad()

    var playButton   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    let image = UIImage(named: "playButton.png") as UIImage

    playButton.frame = CGRectMake(0, 0, 100, 100)
    playButton.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/1.7)
    playButton.addTarget(self, action: "transition:", forControlEvents: UIControlEvents.TouchUpInside)
    playButton.setBackgroundImage(image, forState: UIControlState.Normal)

    self.view.addSubview(playButton)

}

func transition(sender:UIButton!)
{

    println("transition")
    let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("GameViewController") as UIViewController

    secondViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
    self.presentViewController(secondViewController, animated: true, completion: nil)
}

【问题讨论】:

    标签: ios swift sprite-kit viewcontroller skscene


    【解决方案1】:

    我自己找到了一种在多个ViewController 之间导航的方法。 我将 secondViewController 设置为 rootViewController 在我的 PresentationViewController 文件 func transition() 中替换此代码:

    self.presentViewController(secondViewController, animated: true, completion: nil)` 
    

    通过此代码(带选项):

    secondViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
    let window = UIApplication.sharedApplication().windows[0] as UIWindow
    UIView.transitionFromView(
        window.rootViewController!.view,
        toView: secondViewController.view,
        duration: 0.65,
        options: .TransitionCrossDissolve,
        completion: {
            finished in window.rootViewController = secondViewController
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 2017-03-16
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      相关资源
      最近更新 更多