这是 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
,我就无法让它工作。
还有人遇到这个问题吗?有什么解决方法吗?
更新
即使有 @Danilo 的建议,它仍然显示为灰色:
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
呈现的SKScene
;然后我们调整/更改 SKScene
的 backgroundColor
。场景将依次添加一个节点。
例如:
//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)
关于ios - 无法更改 SKView 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37635528/