Animating with Elegance: Exploring Phased Animations in SwiftUI 5

Melissa
3 min readOct 18, 2023

In the dynamic world of SwiftUI, animations have always been a cornerstone, adding that extra layer of polish and interactivity to apps. With SwiftUI 5, Apple has further elevated the animation game with the innovative concept of “Phased Animations”. Let’s dive deep into this captivating feature and see how it can transform our app animations. 🚀

What are Phased Animations? 🤔

Phased animations in SwiftUI 5 empower developers to craft animations that transition through a sequence of states or phases, possibly in a loop. Instead of the traditional linear start-to-end animation, these animations can flow through multiple stages, each with its distinct visual flair.

Why Use Phased Animations? 🌟

  1. Complexity Simplified: By segmenting animations into phases, developers can focus on each part individually, making it simpler to create intricate animations without getting overwhelmed.
  2. Enhanced User Experience: Multi-stage animations offer users a dynamic and engaging visual journey, making apps feel more vibrant and interactive.
  3. Flexibility: With phased animations, there’s a high degree of customization at your fingertips. Control the timing, sequence, and duration of each phase to achieve the desired effect.

Diving into Phased Animations 🛠

SwiftUI’s phased animations offer a structured way to create multi-stage animations. Instead of chaining animations or managing them manually, phased animations allow developers to define distinct “phases” that an animation should progress through. This not only simplifies the code but also offers a more organized and readable structure.

To better understand, let’s explore a playful illustration using an animated bear emoji:

import SwiftUI
enum Phase: CaseIterable {
case initial
case rotate
case jump
case fall
var scale: CGFloat {
switch self {
case .initial: return 1.0
case .rotate: return 1.2
case .jump: return 1.4
case .fall: return 1.0
}
}
var angle: Angle {
switch self {
case .initial, .fall: return .degrees(0)
case .rotate: return .degrees(45)
case .jump: return .degrees(0)
}
}
var offset: CGFloat {
switch self {
case .initial, .rotate: return 0
case .jump: return -50
case .fall: return 50
}
}
}
struct ContentView: View {
@State private var startAnimation = false
var body: some View {
Text("🐻")
.font(.system(size: 100))
.phaseAnimator(Phase.allCases, trigger: startAnimation) { content, phase in
content
.scaleEffect(phase.scale)
.rotationEffect(phase.angle)
.offset(y: phase.offset)
}
.onTapGesture {
startAnimation.toggle()
}
}
}

In the code above, tapping on the bear emoji transitions the animation through four distinct phases: initial state, rotation, jumping up, and falling down. Each phase has its own visual transformation, defined within the .phaseAnimator modifier. This approach offers a clear separation of the visual changes and their corresponding animation styles, making it easier to manage and modify.

Tips and Tricks 🎩

  • Looping: For a continuous phased animation, append the .repeatForever(autoreverses: false) modifier after the .phased modifier.
  • Combining Modifiers: Integrate phased animations with other SwiftUI modifiers to craft more complex and detailed animations.
  • Debugging: If your phased animation seems off, break it down. Examine each phase individually to ensure everything functions as expected.

Phased animations in SwiftUI 5 have ushered in a new era of animation possibilities, enabling developers to design intricate and captivating animations with ease. As always, the key lies in practice and experimentation. So, ignite your Xcode, let your imagination soar, and craft animations that truly stand out!

If you found this article insightful, please give it a clap 👏 and follow for more SwiftUI insights and tutorials! 🌌

--

--