【问题标题】:How to add a ViewController to an SKScene如何将 ViewController 添加到 SKScene
【发布时间】: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


【解决方案1】:

答案对于评论来说太长了,但关于这个问题:incorrect argument label in call (have 'frame:', expected 'coder:') 是因为您正在创建一个新的MyViewController 对象并使用frame 的参数标签对其进行初始化,无论您是否拥有它是否作为类的初始化器是个问题。

我们需要查看您的 MyViewController 类以及您设置了哪些初始化程序,因为您的类似乎只有 1 个初始化程序,而且似乎是 init(coder: NSCoder)。 请向我们展示您的MyViewController 课程。

【讨论】:

  • 如果您想发表比最大长度更长的评论,只需将 ... 添加到第一个评论的末尾,然后继续第二个评论。我认为这会更好地作为评论
【解决方案2】:

简单的答案是你不能。 SpriteKit 和 UIKit 是两种不同的动物,它们只能通过 SKView 连接在一起。

【讨论】:

  • 我认为他们正在尝试在他们的 spritekit 游戏中添加一个关卡选择视图,就像 Flappy Bird 的关卡选择视图等一样。但这只是一个措辞不当的问题。
  • 哦,我知道他们想做什么,这就是我评论他的问题的原因
  • 这是给定问题的正确答案。您不能将视图添加到场景中。场景由视图(SKView)呈现......如果不发表评论就无法投票应该真的很好。
  • @Whirlwind,我同意,谢谢你的评论
  • 我同意你们,这是一个正确的答案。也许人们不赞成它,因为它可能应该是评论(但有 FLAG 选项),或者只是他们不理解关于 viewController 的 OP 问题,而不仅仅是查看。我不知道。顺便说一句,这是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多