ios - 更改按钮图像会删除 skscene

标签 ios swift sprite-kit

我的项目是作为 Spritekit 游戏开始的。 我在 Storyboard中的 defaultviewcontroller 顶部放置了一个按钮。 我在 viewwilllayoutsubviews 中呈现了一个场景。

 if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
            // Configure the view.
            let skView = self.view as! SKView
            self.skView = skView
            skView.showsFPS = true
            skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = SKSceneScaleMode.AspectFill
            println("Width: \(scene.frame.width) andHEight: \(scene.frame.height)")
            scene.gameSceneDelegate = self
            sceneStack.append(scene)
            skView.presentScene(scene)
        }

一段时间后,我通过 View Controller 代码转换到一个新场景:

skView.presentScene(scene, transition: transition)

现在场景发生了变化,但我放在 View Storyboard上的按钮仍保留在场景顶部。

在第二个场景中,我像这样更改 UIButton 的图像:

self.lockButton.setImage(UIImage(named: "unlocked-button"), forState: UIControlState.Normal)

现在按下按钮我改变按钮的图像。这令人惊讶地导致场景变回之前的场景。 结果,更改按钮的任何属性都会导致弹出我的新场景并恢复到我最初呈现的场景。

摘要: 简而言之,我想要的是一个单一的 UIViewController,它将在我游戏的每个场景中显示一个通用的 HUD。因此,我不断更改 UIViewController 呈现的场景,同时保持 UIViewController 不变。但是,当我更改按钮的任何属性(通过 Storyboard设置)并显示在 SKScene 上(图像、backgroundImage、标题等)时,它会导致 View 恢复到它显示的初始场景。

最佳答案

问题是: UIViewController 呈现的初始 SKScene 呈现在 viewWillLayoutSubviews 中 由于更改与我的按钮相关的任何属性,viewWilllayoutSubviews 被调用。 将其更改为仅创建一次初始 View 。 一个愚蠢的错误。

关于ios - 更改按钮图像会删除 skscene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32527201/

相关文章:

css - 如何修复 Mobile Safari 上奇怪的文本输入字段错误?

xcode - 检查系统音量是否静音

uiview - 我如何从 SKScene 切换到 UIView

ios - 背景图像显示在模拟器上但不显示在设备上?

ios - iOS中的三重DES解密

ios - 如何在 moveRowAt : sourceIndexPath:? 后删除空白部分

ios - SWIFT 4 UISearchBar 和 UITableView

ios - 事件指示器被按钮遮挡

swift - 隐藏 UITabBar 后重新应用约束

ios - 使用 SpriteKit 在随机位置复制 Sprite