SwiftUI looping hero animation driven by a beat counter: carousel on an arc with an Animatable modifier, a marker sweep via mask, and how to review motion from stills
Built an animated onboarding hero in SwiftUI (iOS 18): a wheel of round photos that turns one place per beat while a receipt-style list prints a line per beat, looping forever.
What worked:
- One integer drives everything. A pure function maps beat -> (week, linesPrinted, showsTotal, wheelTurns). The view keeps @State beat and a .task loop that sleeps hold(at: beat) then increments. Because the list and the wheel are both derived from the same number they cannot drift, and the mapping is trivially unit-testable (including 'the last printed line is the item at the top of the wheel').
- Items riding an arc: do NOT animate each item's offset/scale directly, or they cut straight across. Put the wheel's turn in an Animatable ViewModifier (var turned: Double; var animatableData) and compute angle -> offset (R sin a, R(1-cos a)), scale and rotation inside body. withAnimation/.animation(.spring, value: turns) then interpolates the turn, and every item follows the curve.
- Infinite wheel with a short data set: use 2x the items as places round a full circle so nothing wraps in view, and hide places past ~3.5 steps from the top (opacity 0), otherwise the far side of the circle shows up over the content below.
- Size falloff must be checked mid-turn, not only at rest: two neighbours are closest when both are half a step from the top. A test that walks the turn in 5% steps and asserts a minimum gap caught an overlap the resting layout did not have.
- Pen-style highlight sweep where text changes colour exactly as the marker passes: draw the row once in the normal colour, overlay the same row in the on-highlight colour over the highlight background, and .mask(alignment: .leading) { Rectangle().scaleEffect(x: progress, anchor: .leading) }. Animating foregroundStyle alongside a scaling background instead leaves a low-contrast moment in dark mode.
- Fixed-canvas illustration: lay out at a reference size (e.g. 402 x 424), then GeometryReader -> scaleEffect(min(w/refW, h/refH, cap)). Pin .dynamicTypeSize(.large) and accessibilityHidden(true) on it since it is a picture, and give it idealHeight so a ScrollView fallback (ViewThatFits) still sizes it.
- A ViewBuilder-generic row with @State hasPrinted set in onAppear gives each newly inserted row its own delayed animation without the parent tracking it.
Reviewing motion without watching it: xcrun simctl io booted screenshot at timed sleeps after launch, tiled side by side into one PNG (a 20-line AppKit script), shows composition at each beat and catches mid-transition frames. Reduce Motion can be forced with xcrun simctl spawn booted defaults write com.apple.Accessibility ReduceMotionEnabled -bool YES plus a relaunch; comparing two screenshots with cmp proves nothing moves. Large text: launch arg -UIPreferredContentSizeCategoryName UICTContentSizeCategoryAccessibilityL. Other language: -AppleLanguages "(de)".
Gotchas: with default MainActor isolation, a nonisolated model type cannot read constants from an isolated namespace of timings; mark that one function @MainActor. Test files using CGSize.zero need import CoreGraphics. xcrun simctl io recordVideo output is ~2 MB/s; avconvert --preset Preset1280x720 cuts it to a third.