Updated for Xcode 16.4
Although SwiftUI comes with a selection of transitions built in, it’s also possible to write entirely custom transitions if we want to.
The process takes three steps:
ViewModifier that represents your transition at any of its states.AnyTransition extension that uses your view modifier for active and identity states.transition() modifier.For example, we could write a shape and view modifier combination that lets us mimic the Iris animation in Keynote – it causes a new slide to appear in a circle that grows upwards, a bit like the old Looney Tunes intro sequence.
To demonstrate this in action, I’m going to show you a complete code example that does several things:
ScaledCircle shape that creates a circle inside a rectangle that is scaled according to some animatable data.ViewModifier struct to apply any shape (in our case, the scaled circle) as a clip shape for another view.AnyTransition extension to wrap that modifier in a transition for easier access.Here’s the code, with added comments to explain what’s going on:
struct ScaledCircle: Shape {
// This controls the size of the circle inside the
// drawing rectangle. When it's 0 the circle is
// invisible, and when it’s 1 the circle fills
// the rectangle.
var animatableData: Double
func path(in rect: CGRect) -> Path {
let maximumCircleDiameter = sqrt(rect.width * rect.width + rect.height * rect.height)
let circleDiameter = maximumCircleDiameter * animatableData
let x = rect.midX - circleDiameter / 2
let y = rect.midY - circleDiameter / 2
let circleRect = CGRect(x: x, y: y, width: circleDiameter, height: circleDiameter)
return Circle().path(in: circleRect)
}
}
// A general modifier that can clip any view using a any shape.
struct ClipShapeModifier<T: Shape>: ViewModifier {
let shape: T
func body(content: Content) -> some View {
content.clipShape(shape)
}
}
// A custom transition combining ScaledCircle and ClipShapeModifier.
extension AnyTransition {
static var iris: AnyTransition {
.modifier(
active: ClipShapeModifier(shape: ScaledCircle(animatableData: 0)),
identity: ClipShapeModifier(shape: ScaledCircle(animatableData: 1))
)
}
}
// An example view move showing and hiding a red
// rectangle using our transition.
struct ContentView: View {
@State private var isShowingRed = false
var body: some View {
ZStack {
Color.blue
.frame(width: 200, height: 200)
if isShowingRed {
Color.red
.frame(width: 200, height: 200)
.transition(.iris)
.zIndex(1)
}
}
.padding(50)
.onTapGesture {
withAnimation(.easeInOut) {
isShowingRed.toggle()
}
}
}
}
Download this as an Xcode project
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further for less! Get my all-new book Everything but the Code to make more money with apps, get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn Swift Testing, design patterns, and more.