iOS Animation
Incorporating UIView animations into your project can transform a decent app into an exceptional one. Any subclass that inherits from UIView — labels, text fields, image views, buttons — can be animated, from its size, to its alpha component, to its position in the view. In this tutorial, you will learn how to apply animations to your views and give your app some flair.
Almost any class that inherits from UIView is animatable: labels, image views, and text fields, for example. From the object’s size, to its alpha component, to its position in the view you can attach an animation and set the various parameters such as the length of the animation, the delay before the animation begins (for example if you want to chain it with other animations in a sequence), and animation options (more on this below).
Get Eddie Char’s stories in your inbox
Join Medium for free to get updates from this writer.
Various properties of UIView can be animated. In Xcode, if you Option-click a property in your code, it should tell you if the property is animatable:
To animate the alpha component, set the initial alpha component value. Then wrap the final value within the animate closure of UIView.animate(). The code below makes label disappear in a duration of 1 second:
let label = UILabel()
label.text = "My Label"
label.alpha = 1.0UIView.animate(duration: 1.0) {
label.alpha = 0.0
}
You can animate other properties as well. The code below chains animations together by setting a delay based off of the duration from the previous animation:
import UIKitclass ViewController: UIViewController {
var label = UILabel() override func viewDidLoad() {
super.viewDidLoad() label.text = "Hammer"
label.frame = CGRect(x: 0,
y: view.frame.height / 2 - 20,
width: 100,
height: 40) view.addSubview(label)
} override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated) //1
UIView.animate(withDuration: 1.0,
delay: 0.0,
options: .curveEaseIn,
animations: {
self.label.frame.origin.x = self.view.frame.width / 2 - 50
}, completion: nil) //2
UIView.animate(withDuration: 0.5,
delay: 1.5,
options: .curveEaseInOut,
animations: {
self.label.transform = CGAffineTransform(
rotationAngle: .pi / 2)
}, completion: nil) //3
UIView.animate(withDuration: 1.5,
delay: 2.0,
usingSpringWithDamping: 0.1,
initialSpringVelocity: 20,
options: .curveEaseInOut,
animations: {
self.label.transform = CGAffineTransform(rotationAngle: 0)
}, completion: nil) //4
UIView.animate(withDuration: 1.0,
delay: 3.5,
options: .curveEaseInOut,
animations: {
self.label.alpha = 0.0
}, completion: { _ in
print("Animation complete.")
})
}
}
Here’s what’s happening:
- Animate the label from the left side of the screen to about halfway across in 1 second
- Then rotate the label 90 degrees clockwise in 0.5 second
- Rotate the label back to 0 degrees counter-clockwise in 1.5 seconds. Here I’ve also included 2 more arguments in the animate function: usingSringWithDamping and initalSpringVelocity. These two optional arguments add a “bouncy” effect to your animation. Feel free to experiment with these values to get the look you want in your animation.
- Finally, fade the label out, and print “Animation complete.”
Below is an explanation of the arguments used in the animate() function:
UIView.animate(withDuration: TimeInterval,
delay: TimeInterval,
usingSpringWithDamping: CGFloat,
initialSpringVelocity: CGFloat,
options: UIView.AnimationOptions,
animations: () -> Void,
completion: ((Bool) -> Void)?)- withDuration: the length of the animation in seconds
- delay: adds a delay in seconds before the animation begins
- usingSpringWithDamping: The damping ratio for the spring animation as it approaches its quiescent state. To smoothly decelerate the animation without oscillation, use a value of 1. Employ a damping ratio closer to zero to increase oscillation.
- initialSpringVelocity: For a smooth start to the animation, match this value to the view’s velocity as it was prior to attachment. A value of 1 corresponds to the total animation distance traversed in one second. For example, if the total animation distance is 200 points and you want the start of the animation to match a view velocity of 100 pt/s, use a value of 0.5
- options: various pre-defined options you can attach to your animation that gives it a certain look and feel. When using an option, you can omit the spring spring damping and velocity as they tend to dilute any options you set here. Here are some common options you can use:
.curveLinear: adds an even animation throughout the duration of the animation
.curveEaseIn: causes the animation to begin slowly, then speed up as it progresses
.curveEaseOut: causes the animation to begin quickly, then slows as it completes
.curveEaseInOut: causes the animation to begin slowly, accelerate through the middle of its duration, and then slow again before completing - animations: enter your animatable code within this closure. (Learn more about closures here).
- completion: you can enter code within this closure to execute once the animation code completes, or enter nil to omit the completion closure.
Summary
Adding animations can liven up your app and give it some flair. Almost any property within UIView can be animated; Option-clicking in your code in Xcode will bring up a window that shows you if the property is animatable. Use the UIView.animate() function to add an animation to your property. While you can have a lot of fun customizing animations, you might want to consider these points:
- Less is more. A simple animation can be very effective. Just don’t overdo it.
- Don’t sacrifice performance. While a complex animation looks cool, if it hinders the UI, or if the user has to view it over and over again and it doesn’t really add value, this may affect your app’s user retention rate.
- Use animation to show intent. For example, you can animate hint bubbles to illustrate how your app is to be used, and how it should flow.
Have fun animating!








