Most iOS developers have used the fantastic UIView animateWithDuration
family of functions. But there's another, slightly-lesser known static function on UIView that can help us transition like a pro. Let's check it out:
The function we'll be trying is transitionWithView
. At first glance you'll see it's takes the same duration, options, and closures as its more-popular sister function.
However, instead of applying our changes over time, this function will (conceptually) take a snapshot of our view before and after the work in the animations
** closure** is performed, then visually transition from the first snapshot to the second.
func flip() {
flipped = !flipped
UIView.transitionWithView(
button,
duration: 0.3,
options: .TransitionFlipFromTop,
animations: {
self.button.setTitle(self.flipped ? "ππ»" : "ππ»", forState: .Normal)
},
completion: nil
)
}
The type of transition depends on the animation option we pass in. We can do everything from simple cross-dissolves (fades), to fancy 3D flips. Super handy for simple transitions.
Download a sample project at j.mp/bite193. In it, we flip a thumbs up emoji using this incredibly simple technique.