Logo for tanaschita.com

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

When working with animations in SwiftUI, we sometimes need to delay an animation, for example when showing a UI element to users and then hiding it after a certain amount of time.

Let's jump right in and see how to do that.

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

Let's look at the following example:

@State private var offset = -60.0 // 1.
var body: some View {
ErrorView()
.offset(y: offset)
.onAppear {
// 2.
withAnimation(.bouncy) {
offset = 60.0
} completion: {
// 3.
withAnimation(.bouncy) {
offset = -60.0
}
}
}
}

What we do in the code above is:

  1. Setting the initial offset of the view out of screen bounds so it's not visible at first.
  2. As soon as the view appears, we animate it to a different offset so it becomes visible on the screen.
  3. As soon as the animation is completed, we animate the view back to a non-visible offset.

What is missing in the example is a delay, so the view stays on the screen for a few seconds before bouncing back.

Since delays are built-in into SwiftUI animations, we can achieve this with one simple call. All we need to do is to add a delay to our second animation:

withAnimation(.bouncy.delay(3))
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

Understanding basic animations in SwiftUI

Learn how to animate state changes in SwiftUI views.

17 May 2026 · 4 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum