我正在尝试使用 Swift 创建 3D 游戏。当玩家死亡时,屏幕上应显示一个 UIButton。问题是按钮在按钮显示之前有很大的延迟(大约 5 秒)。我使用了与 SpriteKit 游戏相同的代码。当按钮应该显示时,我尝试在控制台中打印一条消息,这条消息毫不拖延地出现了。
我的按钮:
func createRespawnButton() {
restartButton = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3, height: 50))
restartButton.setTitle("Play Again!", for: UIControlState.normal)
restartButton.setTitleColor(UIColor.cyan, for: UIControlState.normal)
restartButton.center = CGPoint(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 2)
restartButton.addTarget(self, action: #selector(self.restartGame), for: UIControlEvents.touchDown)
self.view.addSubview(restartButton)
}
当两辆车相撞时调用此函数。有人可以帮助我在没有这么大延迟的情况下让我的按钮显示在屏幕上吗? 谢谢!
最佳答案
来自documentation :
For the most part, use UIKit classes only from your app’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your app’s user interface in any way.
你的函数是在主线程中调用的吗? SCNSceneRendererDelegate
委托(delegate)方法在 SceneKit 的渲染队列中被调用。如果这是您检测到碰撞的地方,您将希望将您的函数调用分派(dispatch)到主队列:
DispatchQueue.main.async {
createRespawnButton()
}
关于ios - SceneKit 按钮显示延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40512408/