Swift - 在视图控制器中的 Sprite Kit 场景后面添加背景图像 [英] Swift - Adding background image behind a Sprite Kit Scene in View Controller

查看:24
本文介绍了Swift - 在视图控制器中的 Sprite Kit 场景后面添加背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的视图控制器中添加背景图像,以便我的精灵套件游戏场景可以在前景中.当游戏场景相互转换时,让我的背景图像留在屏幕上对我来说很重要.我遇到的问题是如何将我为我的背景创建的子视图放置在我的 skscene 后面,目前我的背景图像在我的游戏场景前面,您只能看到图像 sendSubviewToBack 好像不行.以下是我的 viewDidLoad 谢谢

导入 UIKit导入 SpriteKit类 GameViewController:UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()让场景 = MainMenuScene(size: CGSize(width: 1536, height: 2048))让 skView = self.view as!视窗场景.scaleMode = .AspectFillskView.presentScene(场景)让 backgroundImageView = UIImageView(image: UIImage(named: "bg.jpg"))backgroundImageView.frame = view.framebackgroundImageView.contentMode = .ScaleAspectFillview.addSubview(backgroundImageView)view.sendSubviewToBack(backgroundImageView)}}

解决方案

UIKit 无法完成您所描述的事情,因为您的视图无法在渲染场景的 SKView 后面可见.

罢工>

在 iOS 8+ 中,您可以使用 SKView 的 allowsTransparency 属性.文档说明:

<块引用>

这个属性告诉绘图系统它应该如何处理看法.如果设置为 NO,绘图系统会将视图视为完全opaque,允许绘图系统优化一些绘图操作和提高性能.如果设置为 YES,绘图系统通常将视图与其他内容合成.默认值为此属性为 NO.

但在我看来,这不是最好的解决方案.最好的解决方案是使用 Sprite Kit,但问题是你不能跨场景保留节点.您必须在每个场景过渡期间再次添加节点.这就是为什么我不按照文档描述的方式使用 Apple 的场景转换.相反,我使用我自己的自定义 SKNode 并使用这些节点进行我自己的自定义转换,我强烈建议您这样做.我在这里

详细回答了这个问题

I am trying to add a background image in my view controller so my sprite kit game scenes can be in the foreground. It's important for me for my background image to stay on screen while the game scenes transition to each other. The problem I am having is how to place the subview I created for my background to be behind my skscene, at the moment my background image is in front of my game scene and all you can see is the image sendSubviewToBack doesn't seem to work. The following is my viewDidLoad Thanks

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)

    }
}

解决方案

What you're describing can't be done with UIKit because your view can't be visible behind the SKView which renders your scene.

In iOS 8+ you can use the SKView's allowsTransparency property. Documentation states:

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.

But in my opinion this is not the best solution. The best solution is to use Sprite Kit, but the problem is you can't preserve nodes across scenes. You must add the node again during each scene transition. This is why I do not use Apple's scene transitions the way the documentation describes. Instead I use my own custom SKNodes and make my own custom transitions using those nodes, which I highly recommend you do. I gave a detailed answer about this here

这篇关于Swift - 在视图控制器中的 Sprite Kit 场景后面添加背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆