从SKScene迁移到ViewController

时间:2018-02-16 21:17:05

标签: swift uiviewcontroller sprite-kit skscene

当我触摸SKScene时,我想从我的SKSpriteNode移动到视图控制器,当我触摸SKScene时,我想回到UIButton。它正在从视图控制器到SKScene以及另一种方式,但只是第一次。当我再回到我的场景并再次移动到视图控制器(第二次)时,我可以听到音乐,它会打印viewDidLoad的所有内容,但它不会显示UI元素。我驳回了这个场景,这可能不是问题所在。

以下是从场景移回视图控制器的代码。

let currentViewController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!        
currentViewController.present(ViewController(), animated: false, completion: nil)

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"VC")
let currentViewController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!

currentViewController.present(viewController, animated: false, completion: nil)

1 个答案:

答案 0 :(得分:1)

将与currentViewController相关的代码替换为:

self.view?.window?.rootViewController?.present(viewController, animated: true, completion: nil)

它应该工作。 您还可以在segue中创建Storyboard并将其称为:

self.view?.window?.rootViewController?.performSegue(withIdentifier: "VC", sender: self)

编辑:

我已经尝试了我的应用程序之一的下一个解决方案并且它有效:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "VC")
vc.view.frame = rootViewController.view.frame
vc.view.layoutIfNeeded()

UIView.transition(with: window, duration: 0.3, options: .transitionFlipFromRight, animations:
        {
        window.rootViewController = vc
    }, completion: { completed in
        // maybe do something here
    })

对我而言,它有用,我希望它能解决你的问题:)