将 SKScene 添加到 UIViewController? [英] Adding an SKScene to a UIViewController?
问题描述
我刚刚在界面构建器中创建了一个新的 UIViewController
.它与 UITableViewController
(单元格)相分离.我现在想在这个 UIViewController
中使用一个 SKScene
.这是如何实现的?
I just created a new UIViewController
in interface builder. It is segued to from a UITableViewController
(cell). I now want to work with an SKScene
inside this UIViewController
. How is this accomplished?
顺便提一下...
如果SKScene
占据了整个UIViewController.view
,你能在场景顶部叠加UIKit
对象吗?
If the SKScene
takes up the entire UIViewController.view
can you overlay UIKit
objects on top of the scene?
或者最好在UIViewController.view
里面创建一个单独的UIView
来保存SKScene
,然后布局UIKit
对象围绕这个单独的 UIView
,并让默认的 UIViewController.view
作为所有这些元素的容器?如果采用这种方式,理论上这些UIKit
对象可以被SKScene
访问吗?
Or is it better to create a separate UIView
inside the UIViewController.view
to hold the SKScene
, then layout the UIKit
objects around this separate UIView
, and let the default UIViewController.view
act as a container for all these elements? If this approach is taken, theoretically speaking can these UIKit
objects be accessed by the SKScene
?
推荐答案
- 将 SKView 添加到您的 viewController.
- 将 SKScene 添加到您的 SKView.
您可以从 SKScene 内部覆盖 UIKit 项目.这就是我在游戏中使用集合视图和表格视图的方式.但是,它们需要作为子视图添加到包含您的 SKScene 的 SKView 上.
You can overlay UIKit items from inside your SKScene. This is how I use collection and table views in my games. However they would need to be added as a subview on the SKView holding your SKScene.
SKScene有一个视图属性应该保存一个引用到它所包含的SKView.所以这只是做这样的事情.
SKScene has a view property that should hold a reference to the SKView its contained in. So it would just be a matter of doing something like this.
class MyScene: SKScene {
var someView: UIView
override func didMoveToView(view: SKView) {
someView = //setup view here
view.addSubview(someView)
}
}
然后在这个场景中,您可以随时随地访问您的 UIKit 对象.
Then inside this scene you can access your UIKit objects wherever you like.
这篇关于将 SKScene 添加到 UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!