skip to content

In SwiftUI, how do you let a custom Shape animate two properties together, and what does the @Animatable macro change?

level: middleimportance: nice to knowfreq 25%

answer

  1. one animatableData per type
  2. a pair that is VectorArithmetic
  3. first and second accessors
  4. macro reads stored properties
  5. @AnimatableIgnored for the rest

basics

~20 s

animatableData holds one VectorArithmetic value, so two properties travel in an AnimatablePair (or AnimatableValues on iOS 26). The @Animatable macro, added in the iOS 26 SDK, synthesizes it from stored properties; non-animatable ones need @AnimatableIgnored.

solid answer

~40 s

A `Shape` is already `Animatable`, but SwiftUI interpolates only its single `animatableData` property, which must be `VectorArithmetic`. For two values, such as an arc's `start` and `end`, you return `AnimatablePair(start, end)` from the getter and copy `newValue.first` and `newValue.second` back in the setter; three values mean nesting pairs. On iOS 26 and later, `AnimatableValues` holds any number of values. The `@Animatable` macro, introduced with the iOS 26 SDK but declared available from iOS 13, writes that property for you from every stored property whose type is `VectorArithmetic` or `Animatable`, and reports an error for anything else until you mark it `@AnimatableIgnored`. A hand-written `animatableData` is still the tool for clamping or normalising.

code

swift · 27 lines
swift
import SwiftUI

struct ArcSegment: Shape {
    var start: Double      // fraction of a full turn, 0...1
    var end: Double
    var clockwise = false  // not animatable

    var animatableData: AnimatablePair<Double, Double> {
        get { AnimatablePair(start, end) }
        set {
            start = newValue.first
            end = newValue.second
        }
    }

    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.addArc(
            center: CGPoint(x: rect.midX, y: rect.midY),
            radius: min(rect.width, rect.height) / 2,
            startAngle: .degrees(start * 360 - 90),
            endAngle: .degrees(end * 360 - 90),
            clockwise: clockwise
        )
        return path
    }
}

go deeper

for a junior

Recall that a custom Shape animates only what its animatableData exposes, and that two values go into an AnimatablePair.

for a middle

Explain the VectorArithmetic requirement, how the pair's getter and setter work, and what the @Animatable macro and @AnimatableIgnored synthesize or exclude.

for a senior

Show you choose between the macro, AnimatableValues and a hand-written property by deployment target and by whether the setter needs clamping or derived values.

for a principal

Decide when a team should adopt the macro across shared drawing components, given its SDK requirement and how it changes review of animation code.

## One property carries all the animated data The `Animatable` protocol has a single requirement that matters here: **`animatableData`**, a property whose type conforms to **`VectorArithmetic`**. When a change happens inside `withAnimation` or under `animation(_:value:)`, SwiftUI reads the old and new `animatableData`, interpolates between them with vector arithmetic, and calls the setter on every frame so your type can update its stored values. `Shape` already conforms to `Animatable`, so a custom shape only has to expose the right data. The catch is the word **single**. A ring segment whose start and end both move needs two numbers to travel together, but there is only one `animatableData`. `VectorArithmetic` conformers built into SwiftUI include `Double`, `Float` and `CGFloat`; `Int` and `Bool` are not among them, and neither is a Swift tuple. You therefore need a type that is itself `VectorArithmetic` and holds several values. ## AnimatablePair, the long-standing answer **`AnimatablePair<First, Second>`** has been in SwiftUI since iOS 13. It is a pair of `VectorArithmetic` values that is itself `VectorArithmetic`, read through `first` and `second`: - `get` returns `AnimatablePair(start, end)`. - `set` copies `newValue.first` and `newValue.second` back into the stored properties. - For three values you nest: `AnimatablePair<Double, AnimatablePair<Double, Double>>`, which works but quickly becomes hard to read. Any stored property you leave out of the pair, such as a `clockwise` flag, is not interpolated: the shape takes its new value at once and only the paired values move. The getter and setter must stay symmetric: whatever the getter packs into `first` and `second`, the setter must unpack into the same properties, or the shape draws mixed-up values mid-animation. ## AnimatableValues and the @Animatable macro Two newer tools remove most of that boilerplate: | Tool | Available | What it does | |---|---|---| | `AnimatablePair` | iOS 13 | Two values, nested for more | | `AnimatableValues<each Value>` | iOS 26 at runtime | Any number of values in one variadic type, read through `value.0`, `value.1` | | `@Animatable` macro | Introduced with the iOS 26 SDK; its `@available` line reads iOS 13 | Synthesizes `animatableData` and the conformance | | `@AnimatableIgnored` | Same as the macro | Keeps one stored property out of the synthesized data | Apple's documentation describes the macro this way: it "inspects every stored property"; properties whose types conform to `VectorArithmetic` or `Animatable` become part of the synthesized `animatableData`. If a property cannot participate, the macro **emits an error** that suggests marking it with `@AnimatableIgnored` or conforming its type to `VectorArithmetic` or `Animatable`. So a `Bool` flag on an `@Animatable` shape is a compile error until you mark it, never a silent skip. Because the macro's declaration is available from iOS 13, what you need is the newer SDK (Xcode 26 or later), not an iOS 26 deployment target. `AnimatableValues` is different: it is declared for iOS 26 and later, so on an iOS 17 deployment target `AnimatablePair` remains the hand-written choice. ## When to keep writing animatableData by hand The documentation is explicit that a handwritten property is still the right tool when "the interpolated value needs custom logic that does not correspond one-to-one with a stored property, such as normalization, clamping, or driving a derived value." Examples: 1. Clamping an amplitude between zero and a maximum while a bouncy spring overshoots. 2. Wrapping a phase angle with `truncatingRemainder(dividingBy:)` so it never grows without bound. 3. Animating a value the shape derives other values from, rather than every stored field. ## Beyond shapes: modifiers and views The same protocol works on a `ViewModifier` or a `View`: conform it to `Animatable` (or annotate it with `@Animatable`) and SwiftUI sets the interpolated data each frame. Older code uses the `AnimatableModifier` protocol for this; the iOS 27 SDK marks it for deprecation with the message "use Animatable directly", so new code conforms the modifier to `Animatable` itself. ## Mistakes interviewers listen for - Declaring two `animatableData` properties, which is impossible: there is one requirement. - Expecting a tuple or an `Int` to interpolate; neither conforms to `VectorArithmetic`. - Using `AnimatableValues` in an app that still deploys to iOS 17 or 18 without an availability check. - Believing the macro silently skips unsupported properties, when it reports an error. - Assuming an ignored property is frozen; it still changes, just without interpolation.

  • How would you animate three values on an iOS 17 deployment target?
    Nest pairs: `AnimatablePair<Double, AnimatablePair<Double, Double>>`, reading `first`, `second.first` and `second.second`. It is verbose but works everywhere. The `@Animatable` macro is declared available from iOS 13, so it needs the iOS 26 SDK or later to build rather than a newer target; `AnimatableValues` is the one you cannot use there without an availability check.
  • Why would you keep a hand-written animatableData when the macro exists?
    When the animated value is not a one-to-one copy of stored properties. Apple's documentation names normalisation, clamping and driving a derived value: clamping an amplitude while a spring overshoots, or wrapping a phase angle with `truncatingRemainder(dividingBy:)`. The setter is where that logic runs on every frame.

  • A Shape can declare two animatableData properties, one per value.
  • SwiftUI interpolates every stored property of a Shape without any animatableData.
  • A Swift tuple of two Doubles works as animatableData.
  • The @Animatable macro silently skips properties it cannot animate.
  • AnimatableValues works on any deployment target because it is a plain struct.
  • A property marked @AnimatableIgnored can no longer change.

Assumes the iOS 27 SDK: @Animatable and @AnimatableIgnored came with the iOS 26 SDK but are declared from iOS 13, while AnimatableValues needs iOS 26 at runtime.