【发布时间】:2016-12-04 04:15:35
【问题描述】:
我的 SpriteKit 游戏中有以下视图控制器:
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?
更新
我正在尝试使用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)
-
另外,你的 UIViewController 子类是 xib 还是故事板?
-
self.view?.addSubview(myViewController.view)已解决其中一个错误。另一个呢 -incorrect argument label in call (have 'frame:', expected 'coder:')?另外我正在以编程方式创建我的视图控制器,我更喜欢这种方法而不是情节提要。 -
您对很多事情都感到困惑。你需要看一些教程,了解什么是 ViewController,View 是,SKScene 是什么,以及 SKNodes 是什么。然后你需要了解它们是如何相互作用的。我建议按照我给你的顺序学习这些对象。除非有人愿意就此事写大量信息,否则 SO 答案不会帮助您。
-
看看这个答案:stackoverflow.com/a/40676759/6728196。你需要知道这些东西。
标签: swift uiviewcontroller sprite-kit swift3