使用 Apple 最新框架编写 Swift 动画代码 — SwiftUI 动画、Core Animation 与 UIKit。优先使用第一方 API 而非第三方库...
Write animation code that uses Apple's frameworks directly. Third-party animation libraries add dependency risk and often lag behind new OS releases — Apple's APIs are well-optimized for the render pipeline and get free improvements with each iOS version.
Check whether the system already handles the motion you need. Apple's HIG: "Many system components automatically include motion, letting you offer familiar and consistent experiences throughout your app." System components also automatically adjust for accessibility settings and input methods — Liquid Glass (iOS 26) responds with greater emphasis to direct touch and produces subdued effects for trackpad. Custom animation can't match this adaptiveness for free, so prefer system-provided motion when it exists.
Skip custom animation when:
.symbolEffect provides the feedback you need.contentTransition(.numericText) handles your data changewithAnimation is sufficientWrite custom animation when:
Choose the right API for the job. Start with SwiftUI animations (simplest, most declarative), drop to UIKit when you need interactive control, and reach for Core Animation only when you need layer-level precision.
NeedAPIWhyState-driven property changeswithAnimation / .animation(_:value:)Declarative, automatic interpolationMulti-step sequenced animationPhaseAnimatorDiscrete phases with per-phase timingPer-property timeline controlKeyframeAnimatorIndependent keyframe tracks per propertyHero transitions between viewsmatchedGeometryEffect + NamespaceGeometry matching across view identityNavigation push/pop with zoom.navigationTransition(.zoom)iOS 18+ built-in zoom transitionCustom view insertion/removalTransition protocol conformanceTransitionPhase-based modifierIn-view content swap.contentTransition()Numeric text, interpolation, opacityScroll-position-based effects.scrollTransitionPhase-driven scroll-linked animationSF Symbol animation.symbolEffect()Bounce, pulse, wiggle, breathe, rotateInteractive/interruptible (UIKit)UIViewPropertyAnimatorPause, resume, reverse, scrubPer-layer property animationCABasicAnimation / CASpringAnimationShadow, border, cornerRadius animationComplex choreography (layers)CAKeyframeAnimation + CAAnimationGroupMulti-property layer animationPhysics simulationUIDynamicAnimatorGravity, collision, snap, attachmentHaptic feedback paired with animation.sensoryFeedback modifierTied to value changesAnimated background gradientsMeshGradient2D grid of positioned, animated colors
Detailed patterns and code examples live in the reference files. Load the one that matches your task:
TaskReferenceSwiftUI declarative animations (withAnimation, springs, phase, keyframe)references/swiftui-animations.mdView transitions (navigation, modal, custom Transition protocol)references/transitions.mdGesture-driven interactive animationsreferences/gesture-animations.mdCore Animation and UIKit animation patternsreferences/core-animation.md
withAnimation, spring parameters, PhaseAnimator, or KeyframeAnimator → swiftui-animations.mdmatchedGeometryEffect, or custom Transition → transitions.mdCABasicAnimation, UIViewPropertyAnimator, layer animations, or bridging SwiftUI↔UIKit → core-animation.mdSprings are the default animation type in modern SwiftUI. Use duration and bounce — not mass/stiffness/damping unless bridging to UIKit/CA.
PresetDurationBounceUse Case.smooth0.50.0Default transitions, most state changes.snappy0.30.15Micro-interactions, toggles, quick feedback.bouncy0.50.3Playful moments, attention-drawing.interactiveSpring0.150.0Gesture tracking, drag followingCustomvariesvaries.spring(duration: 0.4, bounce: 0.2)
Apple's HIG: "Make motion optional" and "supplement visual feedback by also using alternatives like haptics and audio to communicate." Every animation must handle Reduce Motion, and important state changes should use multiple feedback channels — not animation alone.
swift
@Environment(\.accessibilityReduceMotion) private var reduceMotion
// Pattern 1: Conditional animation
withAnimation(reduceMotion ? .none : .spring()) {
isExpanded.toggle()
}
// Pattern 2: Simplified alternative
.animation(reduceMotion ? .easeOut(duration: 0.15) : .spring(duration: 0.5, bounce: 0.3), value: isActive)
// Pattern 3: Skip entirely
if !reduceMotion {
view.phaseAnimator(phases) { /* ... */ }
}
Reduce Motion fallback options (from most to least graceful):
.animation(.none) or skip the animation block entirelyApple's HIG: "Don't make people wait for an animation to complete before they can do anything, especially if they have to experience the animation more than once." Every animation must be interruptible.
drawingGroup() moves rendering to Metal.id() modifier) — this destroys and recreates the viewgeometryGroup() when parent geometry changes cause child layout anomalies during animationshadowPath when animating shadows — without it, the system recalculates the path every framePhaseAnimator and looping animations lightweight — they run continuouslyRun in order; satisfy each Pass before treating the task as done.
accessibilityReduceMotion (or you only used system defaults / no custom timing).allowsHitTesting(false) for the whole animation unless the task explicitly requires it and a short comment says why.PhaseAnimator always-on, per-cell blur/shadow, or other Performance Checklist red flags. Pass: You ran Instruments (Animation Hitches) and captured a note or path to the trace, or you simplified the pattern first and can point to the change.