Nickoflaus Nickoflaus - 4 months ago 23
Swift Question

Fade animation Swift

How can I make a fade animation from one View controller to the next. So basically what I want is when I click a button, it fades from the current view controller to the one it's linked to. Is there a way to do this. I've been searching for a while and haven't been able to find anything in Swift. Thank you for the help!

Answer

Here is my answer. Instead of making every scene a different ViewController, I just made every scene an SKScene and used one ViewController for the entire project. Now, to get from one scene to another I used this:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        // Get the location of the touch in this scene
        let location = touch.location(in: self)
        // Check if the location of the touch is within the button's bound

        if L1.contains(location) {
            let gameScene = GameScene()
            gameScene.scaleMode = .resizeFill
            gameScene.view?.ignoresSiblingOrder = true
            let transition = SKTransition.fade(withDuration: 1)
            self.scene?.view?.presentScene(gameScene, transition: transition)
            print("tapped!")
        }
}

This allowed me to use the line "self.scene?.view?.presentScene(gameScene, transition: transition" to achieve this transition. The reason I transferred over from view controllers was not only for the animations but also to make the app more efficient and to have it work better and be smoother.