【问题标题】:Unable to change SKView backgroundColor无法更改 SKView 背景颜色
【发布时间】:2016-10-04 18:26:41
【问题描述】:

这是在 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 不起作用。

【问题讨论】:

  • 尝试设置 opaque = NO
  • @Danilo 仍然是灰色的 :(

标签: ios swift cocoa-touch sprite-kit


【解决方案1】:
    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)

【讨论】:

    【解决方案2】:

    除了添加一个SKView,我们还需要一个SKScene,它由SKView呈现;然后我们调整/更改SKScenebackgroundColor。反过来,场景将添加一个节点。

    例如:

    //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 没有效果,但如果你尝试将它设置为任何东西,那么 allowsTransparency = true 不起作用。
    猜你喜欢
    • 2014-03-29
    • 2014-08-21
    • 2013-03-14
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多