簡體   English   中英

無法更改 SKView backgroundColor

[英]Unable to change SKView backgroundColor

這是 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我就無法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 ,我們還需要一個SKSceneSKView呈現; 我們再調整/改變backgroundColor中的SKScene 反過來,場景將添加一個節點。

例如:

//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)
    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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM