SwiftUI animations: withAnimation and transitions explained
SwiftUI animations explained: how to use withAnimation, the animation modifier, transitions and PhaseAnimator, with short code examples for each.
In SwiftUI you animate a view by wrapping a state change in withAnimation or by attaching the .animation(_:value:) modifier. SwiftUI then works out the transition between the old and the new state by itself. You only describe the start and the end.
Three building blocks take you a long way: withAnimation for actions, transitions for views that appear or disappear, and ready-made effects for symbols and numbers.
withAnimation: animate a change
Everything that changes inside the block is animated. Size, color, position and opacity blend smoothly into each other:
struct CardView: View {
@State private var isOpen = false
var body: some View {
RoundedRectangle(cornerRadius: 24)
.fill(.blue)
.frame(height: isOpen ? 320 : 120)
.onTapGesture {
withAnimation(.snappy) {
isOpen.toggle()
}
}
}
}Since iOS 17 there are three well-tuned springs that cover almost everything: .smooth with no bounce, .snappy with a very slight one and .bouncy with a clear bounce. Classic curves like .easeInOut(duration:) are still available.
.animation(_:value:): tie it to a value
If a view should always animate whenever a certain value changes, no matter where the change comes from, attach the animation to the view. Numbers get their own transition where the digits roll:
Text("\(score)")
.contentTransition(.numericText())
.animation(.smooth, value: score)The value parameter matters. The older form without it animates anything that changes at all and quickly leads to unwanted motion.
Transitions: show and hide views
When a view appears or disappears through an if statement, a transition defines how that looks. Several can be combined:
if showsHint {
HintView()
.transition(.move(edge: .bottom).combined(with: .opacity))
}The transition only takes effect if the change to showsHint is animated, for example inside withAnimation. That is the most common reason a transition seems to do nothing.
Ready-made effects: symbols and PhaseAnimator
SwiftUI ships dedicated effects for SF Symbols. For repeating sequences with several steps there is PhaseAnimator, which runs through a list of values one after another:
Image(systemName: "heart.fill")
.symbolEffect(.bounce, value: likeCount)
Circle()
.phaseAnimator([1.0, 1.2, 1.0]) { content, scale in
content.scaleEffect(scale)
}Without a trigger, PhaseAnimator loops. With a trigger it only starts when a value changes, for example as a brief pulse after an action.
Tips for good animations
- Keep them short: Most interface transitions last well under half a second. Longer ones quickly feel sluggish.
- Motion with a purpose: An animation should show where something comes from or what changed. Pure decoration wears off.
- Springs over curves: Springs feel more natural and can be reversed cleanly in mid-motion.
- Test on real devices: Speed and stutter often feel different in the simulator.
Some people don't tolerate a lot of motion on screen and turn it off in the accessibility settings. Respect that setting:
@Environment(\.accessibilityReduceMotion) private var reduceMotion
withAnimation(reduceMotion ? nil : .bouncy) {
isOpen.toggle()
}How animations fit into sheets and lists is shown in SwiftUI sheet and SwiftUI NavigationStack. And how the current design handles motion is covered in Liquid Glass in SwiftUI.