无法更改 SKView 背景颜色 [英] Unable to change SKView backgroundColor
问题描述
这是 iOS 9.我无法设置 SKView 的背景颜色,它总是以默认灰色呈现.有没有办法解决这个问题?
This is with iOS 9. I am unable to set the background color of an SKView, it always renders with the default grey. Is there a way around this?
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
,我就无法让它工作.
When the above code is run the SKView is grey instead of blue. What I really want to be able to do is set allowsTransparency = true
but I can't get that to work if I can't change the background color to clearColor
.
还有其他人遇到这种情况吗?有什么解决方法吗?
Anyone else running into this? Any workarounds?
更新
即使有@Danilo 的建议,它仍然显示为灰色:
Even with @Danilo's suggestion, this still displays as grey:
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
不起作用.
Apparently setting the backgroundColor
of an SKView has not effect, but if you do set it to anything then allowsTransparency = true
doesn't work.
推荐答案
除了添加一个SKView
,我们还需要一个SKScene
,由<代码>SKView;然后我们调整/更改 SKScene
的 backgroundColor
.场景将依次添加一个节点.
In addition to adding a SKView
, we need a SKScene
that is to be presented by the SKView
; we then adjust/change the backgroundColor
of the SKScene
. A node, in turn, will be added by the scene.
例如:
//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)
这篇关于无法更改 SKView 背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!