Animating a Button

Let’s add one final animation touch to the application. When the current mood is changed, also animate the add mood button’s background color. You will use the same property animator technique that you used above.

Modify the property observer for currentMood to animate the button’s background color.

Listing 19.9  Animating the button’s background color (MoodSelectionViewController.swift)

var currentMood: Mood? {
    didSet {
        guard let currentMood = currentMood else {
            addMoodButton?.setTitle(nil, for: .normal)
            addMoodButton?.backgroundColor = nil
            return
        }

        addMoodButton?.setTitle("I'm (currentMood.name)", for: .normal)
        addMoodButton?.backgroundColor = currentMood.color

        let selectionAnimator = UIViewPropertyAnimator(duration: 0.3,
                                                       dampingRatio: 0.7) {
            self.addMoodButton?.backgroundColor = currentMood.color
        }
        selectionAnimator.startAnimation()
    }
}

Build and run the application one last time. Select different images, and the add mood button’s background color will animate as well.

Over the past three chapters, you have used container views to separate different responsibilities of a screen into multiple view controllers, you have implemented a custom control that can be reused across projects, and you have used animations to give a little more polish to your user interfaces. Congratulations!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.150.163