问题描述
这是在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
,但是如果我不能将背景色更改为<$ c $,我将无法正常工作c> 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
更新
显然设置<$ c $ SKView的c> 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
,我们还需要 SKView
将显示的> SKScene ;然后,我们调整/更改 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 backgroundColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!