[英](Swift2 SpriteKit) How to transition between an SKScene and a UIViewController efficiently?
There seems to be so many contradicting ideas on this topic. 关于这个话题似乎有很多矛盾的想法。 I simply wish to have my menu in a UIViewController and my game in the SKScene 我只是希望将菜单放在UIViewController中,将游戏放在SKScene中
In my SKScene I used: 在我的SKScene中,我使用了:
self.removeFromParent()
self.view?.presentScene(nil)
The nodes are removed but the scene is still in place as I still have the grey background and fps counter. 删除了节点,但是场景仍然存在,因为我仍然具有灰色背景和fps计数器。 Can I return to the View aspect of the UIViewController and hide the scene? 我可以返回UIViewController的View方面并隐藏场景吗?
My method one implementation: 我的方法一实现:
RootViewController: RootViewController:
class RootViewController: UIViewController {
var menu = MenuViewController()
var game = GameViewController()
override func viewDidLoad() {
super.viewDidLoad()
print("root")
MenuPresent()
}
func GamePresent() {
self.addChildViewController(game)
self.view.addSubview((game.view)!)
game.didMoveToParentViewController(self)
}
func MenuPresent() {
self.addChildViewController(menu)
self.view.addSubview((menu.view)!)
menu.didMoveToParentViewController(self)
}
func menuDismiss() {
menu.willMoveToParentViewController(nil)
menu.removeFromParentViewController()
menu.view.removeFromSuperview()
}
} }
MenuViewController: MenuViewController:
class MenuViewController: UIViewController {
//var root = RootViewController()
override func viewDidLoad() {
super.viewDidLoad()
print("menu")
}
}
The print("menu") appears in my console, But the actual view and all assets of the MenuViewController do no appear. print(“ menu”)出现在我的控制台中,但是菜单ViewController的实际视图和所有资产都没有出现。
On the other hand my GameViewController and it's SKScene work fine. 另一方面,我的GameViewController及其SKScene工作正常。
Views and scenes are two different things. 视图和场景是两个不同的事物。 Scenes are held inside of a view. 场景保存在视图内部。 You would have to simply present a scene within your menu view or transition to another view and present an SKScene from there. 您只需要在菜单视图中呈现一个场景或过渡到另一个视图,然后从那里呈现一个SKScene。 The code to present the scene might look like this: 呈现场景的代码可能如下所示:
let scene = GameScene(fileNamed: "GameScene")
let skView = self.view as! SKView
scene?.scaleMode = .AspectFit
skView.presentScene(scene)
First you need to know that SKScene
and UIViewController
are totally two different things. 首先,您需要知道SKScene
和UIViewController
完全是两件事。 The hierarchy is typically as following: 层次结构通常如下:
UIViewController
--> UIView
( SKView
) --> SKScene
UIViewController
> UIView
( SKView
)-> SKScene
So you SKScene
is presented in a SKView
which can be a UIView
, then the UIView
is presented in a UIViewController
. 因此,将SKScene
呈现在可以是UIView
的SKView
中,然后将UIView
呈现在UIViewController
。
Once you know the hierarchy, everything is easy. 一旦了解了层次结构,一切就变得容易了。 There are many ways to use different UIViewController
for menu and GameScene
stuff. 有很多方法可以将不同的UIViewController
用于菜单和GameScene
。
Method One 方法一
For example, you can have a RootViewController
, a GameViewController
and a MenuViewController
. 例如,您可以具有RootViewController
, GameViewController
和MenuViewController
。 The RootViewController
is the initial ViewController when the app launches. RootViewController
是应用程序启动时的初始ViewController。
In the RootViewController
, you can create a function to present the GameViewController
: 在RootViewController
,您可以创建一个函数来显示GameViewController
:
func setupGameViewController() {
self.gameViewController = GameViewController()
self.addChildViewController(gameViewController!)
self.view.addSubview((gameViewController!.view)!)
gameViewController?.didMoveToParentViewController(self)
}
You need to present you SKScene
in GameViewController
, I guess you should be familiar with this step. 您需要为您呈现SKScene
在GameViewController
,我想你应该熟悉这一步。
Then when you need to display the menu, you can add the MenuViewController
to RootViewController
with a function like: 然后,当需要显示菜单时,可以使用以下功能将MenuViewController
添加到RootViewController
:
func setupMenuViewController() {
self.menuViewController = MenuViewController()
self.addChildViewController(menuViewController!)
self.view.addSubview((menuViewController!.view)!)
menuViewController?.didMoveToParentViewController(self)
}
You also need to present you menuView in this ViewController, which I suppose you already know. 您还需要在此ViewController中显示menuView,我想您已经知道了。
Also create a function to dismiss the MenuViewController
: 还创建一个函数来关闭MenuViewController
:
func removeMenuViewController(){
self.menuViewController?.willMoveToParentViewController(nil)
self.menuViewController?.removeFromParentViewController()
self.menuViewController?.view.removeFromSuperview()
}
And everything is done. 一切都完成了。
Method Two 方法二
You can also have only one UIViewController
, but create you menu as a UIView
, then you can use self.view.addSuview(menuView)
to present your menu. 您还可以只有一个UIViewController
,但是可以将菜单创建为UIView
,然后可以使用self.view.addSuview(menuView)
展示菜单。 Of course you root view, which is the self.view as SKView
is still there, but it doesn't matter because it's hidden behind the menuView
. 当然,您可以使用root视图,因为它是self.view as SKView
, self.view as SKView
仍然存在,但是没关系,因为它隐藏在menuView
的后面。
A Note for your updated question 有关更新问题的注释
You cannot remove the scene because the scene is actually self.view as SKView
, self.view
is the root view of a UIViewController, it cannot be removed. 由于现场其实是无法删除的场景self.view as SKView
, self.view
是一个UIViewController的根本观点,它不能被删除。 If you really want to remove you scene (for most cases, it's not necessary), you can create a new SKView
that present your SKScene
, then add this SKView
to your UIViewController
by self.view.addSubview(skView)
, when you want to completely remove the scene, just use skView.removeFromSuperview()
. 如果您确实要删除场景(大多数情况下没有必要),则可以创建一个新的SKView
来展示您的SKScene
,然后在需要时通过self.view.addSubview(skView)
将此SKView
添加到UIViewController
。完全删除场景,只需使用skView.removeFromSuperview()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.