ios - 无法更改 SKView 背景颜色

标签 ios swift cocoa-touch sprite-kit

这是 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之外,我们还需要一个由SKView呈现的SKScene;然后我们调整/更改 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)

关于ios - 无法更改 SKView 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37635528/

相关文章:

iphone - 在以编程方式移动的 ScrollView 中检测到 "end decelerating"

ios - Firebase 数据库结构

ios - 为什么 didReceiveRemoteNotification :fetchCompletionHandler is called but usual didReceiveRemoteNotification isn't?

ios - 如何使用 AVAudioPlayer 播放 'http:/......./mp3'

ios - 具有多个目标的 XCode 项目的启动和图标图像文件的位置

ios - 使用 AES 进行部分加密 - IOS 应用程序

ios - 'NSLog' 不可用 : Variadic function is unavailable in swift

json - 转到 json 文件中的字符#

objective-c - 当 UIScrollview 作为 subview 存在时,UIWebview scrollsToTop 不起作用

iphone - XCode 中 iPhone 4 启动屏幕的尺寸 + 名称和图标