首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法更改SKView backgroundColor

无法更改SKView backgroundColor
EN

Stack Overflow用户
提问于 2016-06-04 22:14:34
回答 2查看 1.4K关注 0票数 2

这是iOS 9。我无法设置SKView的背景色,它总是以默认的灰色呈现。有办法绕道吗?

代码语言:javascript
复制
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的建议,这仍然显示为灰色:

代码语言:javascript
复制
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

更新

显然,设置backgroundColor的SKView没有效果,但是如果您将其设置为任何东西,那么allowsTransparency = true就无法工作。

EN

回答 2

Stack Overflow用户

发布于 2016-06-04 23:40:22

除了添加一个SKView之外,我们还需要一个由SKView表示的SKScene;然后调整/更改SKScenebackgroundColor。反过来,一个节点将被添加到场景中。

例如:

代码语言:javascript
复制
//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)
票数 4
EN

Stack Overflow用户

发布于 2016-06-04 23:53:21

代码语言:javascript
复制
    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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37635528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档