UIView.animate is the main method for animating UIView properties in iOS, implemented in UIKit through blocks with duration, delay, and options. The method animates frame, alpha, transform, backgroundColor, and other animatable properties. This article describes the syntax, parameters, practical examples, and the internal structure of UIKit animations.
Key Takeaways
UIView.animate is a static method of the UIView class, introduced in iOS 4 (2010). It provides a declarative API for animating animatable properties of UIView: frame, bounds, center, alpha, transform, backgroundColor. The method accepts a block (closure) with property changes — the system automatically creates CABasicAnimation for each changed property.
The block syntax replaced the old beginAnimations/commitAnimations (iOS 2-3) and became the animation standard in UIKit. UIView.animate animations run on the main thread and do not block the UI. By default, the animation duration is 0.25 seconds with an easeInOut curve.
According to Apple WWDC 2023, the recommended animation approach in new projects is SwiftUI with .animation() and .transition() modifiers, but UIView.animate remains the primary tool for iOS 16 and below support, as well as for complex custom transitions.
UIView.animate provides three key parameters: duration (length in seconds), delay (delay before start), and options (set of UIView.AnimationOptions bit masks). Options include curve (easeInOut, easeIn, easeOut, linear), repeat, autoreverse, allowUserInteraction, beginFromCurrentState, and overrideInheritedDuration.
| Parameter | Type | Description |
|---|---|---|
| duration | TimeInterval | Animation length in seconds (Double type) |
| delay | TimeInterval | Delay before animation starts |
| options | UIView.AnimationOptions | Bit mask: curve, repeat, autoreverse, layoutSubviews |
| animations | () -> Void | Block with changes to animatable properties |
| completion | (Bool) -> Void | Closure after animation completion |
The options parameter is combined via an array: [.curveEaseOut, .repeat, .autoreverse]. Animation with repeat and autoreverse smoothly returns the View to its initial state and repeats indefinitely. For a finite number of repetitions, use animateKeyframes.
Spring animation adds a physical spring effect: the object doesn't just move from point A to B, but overshoots the target with damped oscillations. Parameters: usingSpringWithDamping (damping 0-1, where 0 is maximum oscillation) and initialSpringVelocity (initial spring velocity).
Apple recommends usingSpringWithDamping in the range of 0.4-0.8 for natural UI animations. A value of 0.5 creates noticeable bounce without feeling "rubbery." initialSpringVelocity = 1.0 corresponds to a speed of 1 pt/sec. At 0, the animation starts from rest.
// Spring animation and keyframe example
import UIKit
class AnimatedView: UIView {
func springBounce() {
// Spring animation with damping
UIView.animate(
withDuration: 0.8,
delay: 0,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 1.0,
options: [.allowUserInteraction],
animations: {
self.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
self.alpha = 0.7
},
completion: { _ in
UIView.animate(withDuration: 0.3) {
self.transform = .identity
self.alpha = 1.0
}
}
)
}
func slideIn(from offset: CGFloat) {
self.transform = CGAffineTransform(translationX: offset, y: 0)
self.alpha = 0.0
UIView.animate(
withDuration: 0.6,
delay: 0.1,
options: [.curveEaseOut]
) {
self.transform = .identity
self.alpha = 1.0
}
}
}UIView.animateKeyframes allows you to break an animation into multiple steps (keyframes), each with its own relative time and duration. The addKeyframe(withRelativeStartTime:relativeDuration:) method accepts time values from 0 to 1 relative to the total animation duration.
Keyframe animations are useful for multi-stage effects: button pulse (increase → pause → return), sequential element appearance, complex deformations with intermediate states. Each keyframe can have its own animation curve via options.
// Keyframe animation with complex sequence
func complexPulse() {
UIView.animateKeyframes(withDuration: 2.0, delay: 0, options: [.repeat]) {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.25) {
self.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
self.alpha = 0.5
}
UIView.addKeyframe(withRelativeStartTime: 0.25, relativeDuration: 0.25) {
self.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
self.alpha = 0.8
}
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
self.transform = .identity
self.alpha = 1.0
}
}
}
// Animation with nested closures
func animateCardTransition() {
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut]) {
self.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
} completion: { _ in
UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseIn]) {
self.transform = .identity
}
}
}Under the hood, UIView.animate delegates animation to the CALayer of each UIView through Core Animation (CAAnimation). When you change the frame inside the animation block, the system creates CABasicAnimation for the position and bounds properties of the corresponding layer. If alpha is changed — CABasicAnimation for the layer's opacity property.
Core Animation uses a separate render server process (backboardd on iOS) that processes animations at 120 Hz (ProMotion). UIView.animate does not block the main thread — property changes are sent to the render server, which interpolates frames without application involvement.
According to Apple, animations via UIView.animate have an overhead of ~2-3% CPU per changed layer at 60 FPS. For mass animations (20+ objects simultaneously), it is recommended to use CATransaction and manual layer management.
For debugging and visualizing UIKit animations, use Xcode Debug View Hierarchy — it shows active CAAnimations on each layer, their duration, curve, and current state. Understanding the internal structure of Core Animation helps avoid performance issues with complex animations and choose the optimal approach between UIView.animate and direct use of CABasicAnimation.
For debugging and visualizing UIKit animations, use Xcode Debug View Hierarchy — it shows active CAAnimations on each layer, their duration, curve, and current state. Understanding the internal structure of Core Animation helps avoid performance issues with complex animations and choose the optimal approach between UIView.animate and direct use of CABasicAnimation.
Frequently Asked Questions
Animatable properties of UIView: frame, bounds, center, alpha, transform (CGAffineTransform), backgroundColor. Changes to layoutIfNeeded() when modifying Auto Layout constraints are also animated. Other properties cannot be animated through UIView.animate — use CALayer or CADisplayLink for those.
UIView.animate performs one animation from the current state to the target state. UIView.animateKeyframes splits the total duration into several steps (keyframes) with different relative start times and durations. Keyframes are suitable for multi-stage sequences.
UIView.animate animations are processed in the render server (backboardd) — a separate iOS process. The main thread only sends the initial and final property values. Frame interpolation is performed on the GPU through Core Animation at up to 120 FPS.
Call layer.removeAllAnimations() on the UIView — this removes all CAAnimations created under the hood. For more precise control, use CATransaction and animation identifiers via CAAnimation.keyPath.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also