Skip to main content

Switch, case, animate: animate(withDuration:)

About 2 minSwiftiOSArticle(s)bloghackingwithswift.comcrashcourseswiftxcodeappstoreios

Switch, case, animate: animate(withDuration:) ꎀ렚

Hacking with iOS – learn to code iPhone and iPad apps with free Swift tutorials

Learn Swift coding for iOS with these free tutorials – learn Swift, iOS, and Xcode

Switch, case, animate: animate(withDuration:) | Hacking with iOS

Switch, case, animate: animate(withDuration:)

The currentAnimation property can have a value between 0 and 7, each one triggering a different animation. We're going to create a big switch/case block inside tapped(), but we're going to start small and work our way up – the default case will handle any values we don't explicitly catch.

This switch/case statement is going to go inside a new method of the UIView class called animate(withDuration:), which is a kind of method you haven't seen before because it actually accepts two closures. The parameters we'll be using are how long to animate for, how long to pause before the animation starts, any options you want to provide, what animations to execute, and finally a closure that will execute when the animation finishes.

Update your tapped() method to this:

@IBAction func tapped(_ sender: UIButton) {
    sender.isHidden = true

    UIView.animate(withDuration: 1, delay: 0, options: [],
       animations: {
        switch self.currentAnimation {
        case 0:
            break

        default:
            break
        }
    }) { finished in
        sender.isHidden = false
    }

    currentAnimation += 1

    if currentAnimation > 7 {
        currentAnimation = 0
    }
}

Note

Because we want to show and hide the “Tap” button, we need to make the sender parameter to that method be a UIButton rather than Any.

All that code won't do anything yet, which is remarkable given that it's quite a lot! However, it has put us in a position where we can start dabbling with animations. But first, here's a breakdown of the code:

If you run the app now and tap the button, you'll notice it doesn't actually hide and show as you might expect. This is because UIKit detects that no animation has taken place, so it calls the completion closure straight away.