gpt4 book ai didi

ios - 无法更改SKView backgroundColor

转载 作者:行者123 更新时间:2023-12-01 17:48:57 24 4
gpt4 key购买 nike

这是在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外,我们还需要一个由SKScene表示的SKView;然后,我们调整/更改backgroundColorSKScene。节点将依次由场景添加。

例如:

//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 backgroundColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37635528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com