У меня есть следующий контроллер представления в моей SpriteKit игре:Как добавить ViewController к 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. Я считаю, что этот взгляд можно добавить к SKView
из моего класса SKScene
(см. Ниже). Как это может быть сделано?
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 или раскадровку? – nshoute
'self.view? .addSubview (myViewController.view)' решил одну из ошибок. Как насчет другого - 'неправильная метка аргумента в вызове (есть 'frame:', expected 'coder:')'? Также я создаю свой контроллер просмотра программно, я предпочитаю этот подход к раскадровке. – Serenade