gpt4 book ai didi

ios - Swift - 在 View Controller 中的 Sprite Kit 场景后面添加背景图像

转载 作者:行者123 更新时间:2023-11-28 11:17:20 24 4
gpt4 key购买 nike

我正在尝试在我的 View Controller 中添加背景图像,以便我的 sprite 套件游戏场景可以在前景中。当游戏场景相互转换时,我的背景图像留在屏幕上对我来说很重要。我遇到的问题是如何将我为我的背景创建的 subview 放在我的 skscene 后面,此时我的背景图像在我的游戏场景前面,你只能看到图像 sendSubviewToBack 似乎不起作用。下面是我的viewDidLoad 感谢

import UIKit
import SpriteKit

class GameViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()
let scene = MainMenuScene(size: CGSize(width: 1536, height: 2048))
let skView = self.view as! SKView

scene.scaleMode = .AspectFill
skView.presentScene(scene)

let backgroundImageView = UIImageView(image: UIImage(named: "bg.jpg"))
backgroundImageView.frame = view.frame
backgroundImageView.contentMode = .ScaleAspectFill
view.addSubview(backgroundImageView)
view.sendSubviewToBack(backgroundImageView)

}
}

最佳答案

您所描述的内容无法使用 UIKit 完成,因为您的 View 在呈现场景的 SKView 后面不可见。

在 iOS 8+ 中,您可以使用 SKView 的 allowsTransparency 属性。文档状态:

This property tells the drawing system as to how it should treat the view. If set to NO, the drawing system treats the view as fully opaque, which allows the drawing system to optimize some drawing operations and improve performance. If set to YES, the drawing system composites the view normally with other content. The default value of this property is NO.

但在我看来这不是最好的解决方案。最好的解决方案是使用 Sprite Kit,但问题是您无法跨场景保留节点。您必须在每个场景转换期间再次添加节点。这就是为什么我不按照文档描述的方式使用 Apple 的场景转换。相反,我使用我自己的自定义 SKNodes 并使用这些节点进行我自己的自定义转换,我强烈建议您这样做。关于这个我给了详细的回答 here

关于ios - Swift - 在 View Controller 中的 Sprite Kit 场景后面添加背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30957905/

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