---
name: swiftui-motion
description: Use when building animations in SwiftUI, including springs, matched geometry, phase and keyframe animators, symbol effects, scroll transitions and haptics.
---

# SwiftUI motion

Use SwiftUI's built-in animation system first; it is interruptible and handles accessibility.

## Defaults

- `withAnimation(.snappy)` for quick state changes.
- `.smooth` for layout changes, `.bouncy` sparingly.
- Custom: `.spring(response: 0.35, dampingFraction: 0.85)`.

## Shared elements

```swift
@Namespace private var ns
CardView().matchedGeometryEffect(id: item.id, in: ns)
```

Use the same `id` in both states and wrap the state change in `withAnimation`.

## Multi-step animations

- `PhaseAnimator` for simple loops through states.
- `KeyframeAnimator` for timed tracks (scale, rotation, offset) like a bounce or shake.

## Transitions

- `.transition(.move(edge: .bottom).combined(with: .opacity))` for sheets and toasts.
- `.contentTransition(.numericText())` for numbers.
- `.symbolEffect(.bounce, value: count)` and `.contentTransition(.symbolEffect(.replace))` for SF Symbols.

## Scroll

- `.scrollTransition { content, phase in content.scaleEffect(phase.isIdentity ? 1 : 0.92).opacity(phase.isIdentity ? 1 : 0.6) }`
- `.scrollTargetBehavior(.viewAligned)` for paging carousels.

## Haptics

`.sensoryFeedback(.selection, trigger: value)`, `.success` on completion, `.impact(weight: .light)` on snaps.

## Performance

- Use `.drawingGroup()` for heavy layered drawing.
- Use `Canvas` with `TimelineView(.animation)` for particles.
- Avoid animating `frame` on large lists; animate `offset` and `scaleEffect`.

## Accessibility

Read `accessibilityReduceMotion` and fall back to opacity changes.
