tezvyn:

GeometryEffect

AI-drafted, machine-checkedSource: developer.apple.comadvanced
GeometryEffect

GeometryEffect is a SwiftUI protocol for building custom animatable 2D transforms beyond the built-in offset, scaleEffect, and rotationEffect modifiers, by returning a ProjectionTransform that SwiftUI recomputes and animates on every frame.

WHY IT EXISTS SwiftUI ships common transforms as ready-made modifiers: offset, scaleEffect, rotationEffect. But some effects, a text field that shakes side to side to signal an error, a card that appears to curl like a page, are not one of those built-ins, and hand-rolling them with a plain ViewModifier would only snap between states instead of animating. GeometryEffect exists to let a custom geometric transform participate fully in SwiftUI's animation system, interpolating frame by frame exactly like the built-in modifiers do.

THE MENTAL MODEL Writing a GeometryEffect is writing your own version of offset or rotationEffect from scratch. Instead of describing what the view looks like, you hand SwiftUI a transform, a small matrix describing how to warp the view's geometry, for the current instant in an animation. Because the type also conforms to Animatable, SwiftUI recomputes that matrix at every frame as its underlying data interpolates, and reapplies it, the same way it reapplies its own built-in transforms.

HOW IT WORKS GeometryEffect refines both ViewModifier and Animatable. A conforming type implements animatableData for whatever value drives the effect, say a shake offset amount, and a function called effect(size:) that, given the view's current size and the current interpolated animatableData, returns a ProjectionTransform, a wrapper around CGAffineTransform describing the geometric warp to apply right now. SwiftUI calls effect(size:) on every frame during an animation, feeding it the interpolated value for that instant, and composes the resulting transform directly into rendering, without any direct Core Animation code from you.

WHEN IT MATTERS It matters for building custom, physically expressive interactions that no stock modifier covers, like a shake-to-indicate-error field or a folding card. The footgun is doing expensive work inside effect(size:), since SwiftUI can call it well over a hundred times a second during an animation on a ProMotion display, so any heavy computation there shows up as visible jank. A second footgun is reaching for GeometryEffect out of habit when a plain offset or rotationEffect with an animation modifier would already do the job more simply.

ONE CONCRETE EXAMPLE A login field's shake effect stores animatableData as a travel amount, and effect(size:) returns a horizontal ProjectionTransform built from a sine wave of that travel amount scaled by an amplitude. Wrapping the field in the shake modifier and incrementing an attempt count on each failed login triggers a springy side-to-side shake, animated automatically by SwiftUI rather than by hand.

Read the original → developer.apple.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.