从 SKScene 移动到 ViewController
Moving from SKScene to ViewController
我想在触摸 SKSpriteNode
时从 SKScene
移动到视图控制器,在触摸 UIButton
时返回 SKScene
。它从视图控制器工作到 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)
将与currentViewController
相关的代码替换为:
self.view?.window?.rootViewController?.present(viewController, animated: true, completion: nil)
应该可以。
您还可以在 Storyboard
中创建一个 segue
并像这样调用它:
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
})
对我来说它有效,我希望它能解决你的问题:)
我想在触摸 SKSpriteNode
时从 SKScene
移动到视图控制器,在触摸 UIButton
时返回 SKScene
。它从视图控制器工作到 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)
将与currentViewController
相关的代码替换为:
self.view?.window?.rootViewController?.present(viewController, animated: true, completion: nil)
应该可以。
您还可以在 Storyboard
中创建一个 segue
并像这样调用它:
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
})
对我来说它有效,我希望它能解决你的问题:)