我在SpriteKit遊戲下面的視圖控制器:如何將視圖控制器添加到SKScene
import UIKit
class MenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .red
cv.dataSource = self
cv.delegate = self
cv.isPagingEnabled = true
return cv
}()
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
//use autolayout instead
collectionView.anchorToTop(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
collectionView.register(PageCell.self, forCellWithReuseIdentifier: cellId)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath)
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
}
extension UIView {
func anchorToTop(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil) {
anchorWithConstantsToTop(top: top, left: left, bottom: bottom, right: right, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
}
func anchorWithConstantsToTop(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0) {
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: topConstant).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant).isActive = true
}
if let left = left {
leftAnchor.constraint(equalTo: left, constant: leftConstant).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: -rightConstant).isActive = true
}
}
}
我試着在我的SKScene
初始化它以下列方式:
override init(size: CGSize){
super.init(size: size)
weak var myViewController: MyViewController!
myViewController = MyViewController(frame: self.frame)
self.view?.addSubview(myViewController)
}
以上給了我以下錯誤:
"incorrect argument label in call (have 'frame:', expected 'coder:')"
和
"cannot convert type of 'MyViewController!' to expected argument type 'UIView'"
我想我做錯了什麼。如何將我的視圖控制器添加到SKScene
?
UPDATE
我試圖讓使用UICollectionView
,這就是爲什麼我試圖以上(現在顯示的全MenuViewController代碼)一個視圖控制器添加到我的SKScene
我的遊戲菜單。但是現在我明白了,我應該將它添加到我的SKView中。我相信這個觀點可以從我的SKScene
課程(如下)添加到SKView
。如何才能做到這一點?
import SpriteKit
import GameplayKit
import UIKit
class GameScene: SKScene {
private var label : SKLabelNode?
private var spinnyNode : SKShapeNode?
override func didMove(to view: SKView) {
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let label = self.label {
label.run(SKAction.init(named: "Pulse")!, withKey: "fadeInOut")
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
您是否嘗試過self.view?.addSubview(myViewController.view) – nshoute
另外,您的UIViewController子類是xib還是storyboard? – nshoute
'self.view?.addSubview(myViewController.view)'已經解決了其中一個錯誤。另一個呢 - 調用中不正確的參數標籤(有'框架:',預期'編碼器:')'?此外,我正在編程創建我的視圖控制器,我更喜歡這種方法的故事板。 – Serenade