ios - SceneKit 按钮显示延迟

标签 ios swift uibutton scenekit

我正在尝试使用 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/

相关文章:

iphone - Facebook iOS SDK 不返回用户电子邮件

ios - 如何在 iOS7 中停止线程(由 dispatch_queue_create 创建)

ios - 当一个 Action 在 Xcode 的另一个屏幕上完成时如何改变按钮的状态?

ios - Swift:如何根据按钮点击将结果显示到标签

ios - 获取 "nested push animation can result in corrupted navigation bar"

html - iOS 邮件客户端不呈现 html 电子邮件

ios - 如何在 Swift 中显示文档目录中的贴纸

ios - 向 Firebase 节点添加一个值而不是覆盖已经存在的值

ios - 如何修复 iOS14 后未显示的 UITextField 背景图像?

ios - 较小尺寸的按钮不带 layer.cornerRadius