我正在构建一个简单的游戏,它只有 3 个 UIViewController、一个主屏幕、GameViewController 和一个暂停模式。当我在 iPhone 6s 上从 Xcode 启动应用程序时,CPU 使用率从 0% 开始。当我切换到 GameViewController 时,CPU 使用率会飙升,但当我切换回主屏幕时,它不再是 0%,而是会超过 10%。如果我继续来回 segue,主屏幕上的 CPU 使用率每次都会增加 10% 左右。这是我一直用来 segue 的代码:
func pageSegue(_ currentScreen: String) {
let newVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: currentScreen) as UIViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = newVC
}
上面的代码找到它要转场到的 View Controller 的 Storyboard标识符(字符串),然后调用转场。
下面的屏幕截图是 Instruments 在转到 GameViewController 然后仅返回主屏幕一次后的样子。执行此操作后,突出显示的行及其上方的百分比继续攀升。
我也曾尝试将以下代码添加到 viewDidDisappear
但没有成功:
//gameScene is the SKView
gameScene.scene?.isPaused = true
for child in (gameScene.scene?.children)! {
child.removeChildren(in: [child])
}
gameScene.presentScene(nil)
gameScene.removeFromSuperview()
这个问题让我觉得我遗漏了一些关于解除分配 View Controller 的非常基本的东西。感谢您的帮助。
最佳答案
问题是我从模态 VC 调用 pageSegue()
。我最初有这个:
@IBAction func goHome(_ sender: Any) {
pageSegue("HomeScreen")
}
我改成了这样:
@IBAction func goHome(_ sender: Any) {
dismiss(animated: false, completion: {
pageSegue("HomeScreen")
})
}
现在每次调用 segue 时都会释放 GameViewController,并且 CPU 使用率在主屏幕上返回到 0%。
关于swift - SKView 在转到另一个 UIViewController 后继续使用资源 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47192449/