无法更改SKView backgroundColor
这是与iOS 9.我无法设置一个SKView的背景颜色,它总是呈现与默认的灰色。 有没有解决的办法?
let frame = CGRect(x: 0, y: 0, width: 200, height: 100) let spriteView = SKView(frame: frame) spriteView.backgroundColor = .blueColor() self.view.addSubview(spriteView)
当上面的代码运行时,SKView是灰色的而不是蓝色的。 我真正想要能够做的是设置allowsTransparency = true
但我不能让这个工作,如果我不能改变背景颜色clearColor
。
其他人遇到这个? 任何解决方法?
更新
即使有@达尼洛的build议,这仍然显示为灰色:
let frame = CGRect(x: 0, y: 0, width: 200, height: 100) let spriteView = SKView(frame: frame) spriteView.backgroundColor = .clearColor() spriteView.allowsTransparency = true spriteView.opaque = false
更新
显然设置一个SKView的backgroundColor
没有影响,但如果你确实设置了任何东西,那么allowsTransparency = true
不起作用。
除了添加一个SKView
,我们还需要一个由SKView
提供的SKView
; 然后我们调整/更改SKScene
。 反过来,节点将被场景添加。
例如:
//create a SKView, which takes up the same frame as our view let spriteView = SKView(frame: self.view.bounds) //adding spriteView as a child view of our view self.view.addSubview(spriteView) //Here, we create a scene, which is the root object of //the graph let scene = SKScene(size: spriteView.frame.size) scene.backgroundColor = .orangeColor() //As said, we present the scene with our SKView spriteView.presentScene(scene) //Here, we create a node, which will be added by our scene //This node takes up a size that you originally created and has the //background color set to blue let nodeFrame = CGRect(x: 0, y: 0, width: 200, height: 100) let node = SKSpriteNode(color: .blueColor(), size: nodeFrame.size) //The following just serves to show how we can adjust the node's //position; I will leave that to you node.position = CGPointMake(scene.frame.size.width - 200, scene.frame.size.height - 100) //Finally, we add the node to our scene scene.addChild(node)
let spriteView = SKView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) let spriteKitScene = SKScene(size: spriteView.frame.size) spriteKitScene.backgroundColor = SKColor.blackColor() spriteView.presentScene(spriteKitScene) self.view.addSubview(spriteView)