所以我创造了一个3x10的精灵网格,它离开了屏幕的底部边缘。我添加了一个UIScrollView,滚动指示符出现,但实际上没有移动。 这是代码:
import SpriteKit
var buyButton = SKSpriteNode()
var pic = SKTexture(imageNamed: "Button")
class GameScene: SKScene, UIScrollViewDelegate {
var scrollView = UIScrollView()
var scrollFrame = CGRect!()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
print(self.frame.size.width)
scrollView = UIScrollView(frame: CGRect(origin: CGPointMake(0,0), size: CGSizeMake(self.frame.size.width, self.frame.size.height)))
scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height*2)
scrollView.bounces = false
scrollView.pagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = true
scrollView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)
var width = self.frame.size.width
var height = self.frame.size.height
for i in 1...10{
for x in 1...3{
buyButton = SKSpriteNode(texture: pic, size: CGSizeMake(width*13/CGFloat(45), 100))//(rectOfSize: CGSizeMake(width*13/CGFloat(45), 100))
buyButton.position = CGPointMake(CGFloat(29.0)*CGFloat(x)*width/CGFloat(90.0)-width*CGFloat(13)/CGFloat(90), CGFloat(i)*0.15*height-width*CGFloat(13)/CGFloat(45)*9.0/16.0)
print("\(x)\(i)")
buyButton.size = CGSizeMake(width*CGFloat(13)/CGFloat(45), width*CGFloat(13)/CGFloat(45)*9.0/16.0)
buyButton.name = "\(x)\(i)"
self.addChild(buyButton)
var label = SKLabelNode(text: "hi")
label.fontSize = 40
label.name = "Label\(x)\(i)"
buyButton.addChild(label)
print(CGFloat(29.0)*CGFloat(x)*width/CGFloat(90.0)-width*CGFloat(13)/CGFloat(90))
label.fontColor = UIColor.blackColor()
label.horizontalAlignmentMode = .Center
label.verticalAlignmentMode = .Center
}
}
self.scene?.view?.addSubview(scrollView)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
所以我不完全确定我做错了什么。
我意识到这不应该在GameScene.swift文件中,并且应该是分开的,但这只是一个测试项目。
谢谢: - )
答案 0 :(得分:2)
你遇到了一段时间我遇到的问题。根本问题是您无法将SKNode添加到UIKit视图,也无法将UIKit视图添加到SKNodes。视图不是节点,节点不是视图,遗憾的是这不起作用。虽然您可以将UIKit元素添加到SKScene的视图属性中,但这是您可以接受的。在我终于得到它之前,我挣扎了很长一段时间。
我仍然混合使用UIKit和SpriteKit,顺便说一下,但我是以有限的方式这样做的,它从不依赖于向视图添加节点,反之亦然。