Logo for tanaschita.com

Understanding basic animations in SwiftUI

Learn how to animate state changes in SwiftUI views.

17 May 2026 · 4 min read

SwiftUI provides built-in support for animations. In many cases, adding animations only requires attaching an animation to a state change.

There are two common ways to do this:

  1. animation(_:value:) view modifier for animations when a certain state value changes
  2. withAnimation(_:_:) global function which we can use to apply state changes and create animations for all views affected by them

In this article, we'll look at how both approaches work and when to use each of them.

Let's dive in.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

Animating a view with animation(_:value:)

The animation(_:value:) view modifier is useful when we need to animate a view every time a specific value changes. We can use it as follows:

@State private var color = Color.brown
var body: some View {
Button("Change color") {
color = color == .brown ? .mint : .brown
}
.padding(20)
.background(color)
.foregroundStyle(.white)
.animation(.easeInOut, value: color)
}

The modifier does not animate the value itself. The value parameter defines which state change should trigger the animation. Whenever the value of color changes, SwiftUI animates all affected updates.

This type of animation is often referred to as an implicit animation because the view automatically animates when the specified state changes.

We can also disable animation for a view by passing nil:

.animation(nil, value: color)

This is handy when we want to opt a specific view out of an animation that would otherwise be inherited from a parent.

Animating state changes with withAnimation

Instead of attaching the animation to a view, we can also explicitly animate state changes by using the withAnimation global function:

@State private var color = Color.brown
var body: some View {
Button("Change color") {
withAnimation(.easeInOut) {
color = color == .brown ? .mint : .brown
}
}
.padding(20)
.background(color)
.foregroundStyle(.white)
}

In this approach, the animation is applied to all view updates caused by the state changes inside the withAnimation closure.

Reacting when an animation finishes

withAnimation additionally provides a completion handler (iOS 17+), so we can run code once the animation settles:

withAnimation(.easeInOut) {
color = color == .brown ? .mint : .brown
} completion: {
print("Color animation finished")
}

This is useful for chaining animations or triggering follow-up actions after a transition completes.

Choosing between both approaches

Both approaches can often achieve the same visual result, but they are useful in different situations.

animation(_:value:) works well when a specific view should always animate in response to a particular state change.

withAnimation is often the better choice when multiple state changes should animate together or when we want more explicit control over when animations occur.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

swiftui

animations

swift

ios

How to delay an animation in SwiftUI

Learn how to wait for a certain amount of time before starting a SwiftUI animation.

26 Feb 2024 · 2 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum