ich hinzufügen muß die folgenden View-Controller in meinem SpriteKit Spiel:Wie ein Viewcontroller zu einem SKScene
override init(size: CGSize){
super.init(size: size)
weak var myViewController: MyViewController!
myViewController = MyViewController(frame: self.frame)
self.view?.addSubview(myViewController)
}
Die oben:
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
}
}
}
ich es in der folgenden Art und Weise in meinem SKScene
versuchte Initialisierung gab mir die folgenden Fehler:
"incorrect argument label in call (have 'frame:', expected 'coder:')"
und
"cannot convert type of 'MyViewController!' to expected argument type 'UIView'"
Ich denke, ich mache etwas falsch. Wie kann ich meinen View Controller zum SKScene
hinzufügen?
UPDATE
ich ein Menü für mein Spiel zu machen versuchen UICollectionView
verwenden, warum ich einen View-Controller oben (jetzt den vollen MenuViewController Code angezeigt) hinzuzufügen, versuche meinen SKScene
. Aber jetzt bekomme ich die Idee, dass ich es stattdessen zu meinem SKView hinzufügen sollte. Ich glaube, diese Ansicht kann zu der SKView
von meiner SKScene
Klasse (unten) hinzugefügt werden. Wie kann das gemacht werden?
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
}
}
Haben Sie versucht, self.view? .addSubview (myViewController.view) – nshoute
Ist auch Ihre UIViewController Unterklasse ein Xib oder Storyboard? – nshoute
'self.view? .addSubview (myViewController.view)' hat einen der Fehler behoben. Was ist mit dem anderen - "falsche Argumentbezeichnung im Aufruf (habe 'frame:', erwartet 'coder:')'? Außerdem erstelle ich meinen View-Controller programmatisch, ich bevorzuge diesen Ansatz für das Storyboard. – Serenade