Transitioning Between Scenes in SpriteKit

Transitioning between scenes in SpritKit and Swift it relatively straightforward. As a rule of thumb in Swift, each scene should have its own class. For example, I have a LoginScene.class that handles a username and password prompt. Once a user logs in successfully, the code in LoginScene.class transitions to a profile scene called ProfileScene.class. The code to transition looks like this:

let transition: SKTransition = SKTransition.fade(withDuration: 0.5)
let scene: SKScene = ProfileScene(size: self.size)
self.view?.presentScene(scene, transition: transition)

The transition variable allows for a fade of 0.5 seconds. Also of note: if you have UI members in your scene (e.g., a UITextField), you will need to remove them before transitioning to the new scene. If the UI elements were added in the main thread, you'll have to use Grand Central Dispatch to remove the elements from the main thread. Otherwise, an error will be thrown like the following:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.

For example, to remove a UITextField called userNameField from the Superview in the main thread, you could do this:

DispatchQueue.main.async {
    self.userNameField.removeFromSuperview()
}

Then, you can call the new scene with self.view?.presentScene(scene, transition: transition) (from the example above).