steff feyens steff feyens - 1 year ago 135
Swift Question

SceneKit Button show delay

I'm trying to create a 3D game with Swift. When the player dies, a UIButton should show on the screen. The problem is that the button has a very big delay (something like 5 seconds) till the button shows. I've used the same code as I would use for a SpriteKit game. I tried to print a message in the console when the button should show and this message came without delay.

My Button:

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) = 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)


This function is called when 2 cars collide with each other. Can someone help me to make my button show on the screen without such a big delay?

Answer Source

From the 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.

Is your function called in the main thread? SCNSceneRendererDelegate delegation methods are called in SceneKit's rendering queue. If that's where you detect collisions, you will want to dispatch your function call to the main queue:

DispatchQueue.main.async {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download