Advanced Animation Techniques in SwiftUI

Introduction

Deepak Tundwal
3 min readDec 20, 2023

Welcome back to our engaging series on SwiftUI animations! In this article, titled “Advanced Animation Techniques in SwiftUI,” we delve deeper into the world of animations, stepping beyond the basics to explore more sophisticated and nuanced aspects. Building on the foundational knowledge from our main article, this section is crafted for those who are ready to elevate their SwiftUI animation skills to the next level.

Here, we will uncover the potential of advanced animations in SwiftUI, offering insights into creating more complex, refined, and interactive animations. From understanding custom timing curves that add subtlety to your animations, to mastering the art of chaining animations for seamless and intricate sequences, this article is designed to broaden your animation repertoire. We will also explore the fascinating world of spring animations, adding a natural and dynamic feel to your app interfaces.

Whether you are an experienced iOS developer or an enthusiast looking to expand your SwiftUI knowledge, this article aims to provide you with the tools and understanding necessary to create stunning and advanced animations. Let’s dive in and discover how to bring a higher level of sophistication and engagement to your SwiftUI apps!

What Are Timing Curves?
Timing curves define the speed of an animation over time. By adjusting these curves, you can create animations that start slowly and speed up, or start quickly and slow down at the end.

Using timingCurve:
SwiftUI provides a timingCurve function where you can specify control points of a cubic Bézier curve, giving you fine control over the animation’s pacing.

Chaining Animations: Creating Complex Effects

Chaining animations involves sequencing multiple animations one after the other or even overlapping them. This technique is particularly useful for creating elaborate animation sequences.

How to Chain Animations:
You can chain animations using the delay modifier to start an animation after a certain delay, thereby creating a sequence. Another approach is to use Animation’s sequenced(before:after:) method to directly sequence two animations.

Spring Animations: Adding Bounce and Elasticity. Spring animations add a natural, physics-based motion to your animations, making them feel more dynamic.

Implementing Spring Animations:
SwiftUI’s .spring animation modifier allows you to specify parameters like damping and response to control the springiness of the animation.

Code Example: Implementing Advanced Animations

Let’s create an example that combines these advanced techniques:

Note: In iOS 15 and later, Apple recommends using animation(_:value:) for view-based animations. This approach ties the animation more directly to state changes, making it clearer what changes trigger the animation.

import SwiftUI

struct AdvancedAnimationsView: View {
@State private var moveBall = false

var body: some View {
VStack {
Spacer()

Circle()
.frame(width: 50, height: 50)
.foregroundColor(.green)
.offset(y: moveBall ? 300 : -300)
.animation(
Animation.timingCurve(0.2, 0.8, 0.9, 0.3, duration: 2)
.repeatForever(autoreverses: true),
value: moveBall
)

Spacer()

Button("Start Animation") {
withAnimation {
moveBall.toggle()
}
}
}
}
}

struct AdvancedAnimationsView_Previews: PreviewProvider {
static var previews: some View {
AdvancedAnimationsView()
}
}

In this code:

  • We create a Circle view representing a ball.
  • The offset modifier moves the ball up and down.
  • A custom timingCurve animation is applied, creating a unique motion for the ball.
  • The animation is set to repeat indefinitely with repeatForever.
  • The animation(_:value:) modifier is used, where value: moveBall specifies that the animation should be triggered by changes to the moveBall state variable.
  • The withAnimation block in the button’s action ensures that the state change is animated using the specified animation.

This example demonstrates a custom timing curve and repeated animation. Experiment with different control points in the timing curve to see how they affect the motion of the animation.

In this section, we explored advanced animation techniques in SwiftUI, including custom timing curves, chaining animations, and spring animations. The code example provided serves as a practical demonstration of these concepts. As you delve deeper into these techniques, you’ll discover the immense potential of SwiftUI to bring sophisticated and engaging animations to your iOS apps.

--

--