SwiftUI 動畫與轉場
全面的 SwiftUI 動畫指南,包括新的 @Animatable 巨集 (iOS 26)、轉場以及現代 iOS 開發的動態設計最佳實務。
先決條件
- iOS 17+ 用於 PhaseAnimator/KeyframeAnimator
- iOS 26+ 用於 @Animatable 巨集
- Xcode 26+
@Animatable 巨集 (iOS 26 - 全新!)
自訂動畫的革命性變革
iOS 26 引入了 @Animatable 巨集,消除了先前為自訂形狀和視圖製作動畫所需的大量樣板程式碼。
iOS 26 之前 (手動方式)
// 舊方式 - 大量樣板程式碼
struct PieSlice: Shape {
var startAngle: Angle
var endAngle: Angle
// 需要手動實作 animatableData
var animatableData: AnimatablePair<Double, Double> {
get {
AnimatablePair(startAngle.radians, endAngle.radians)
}
set {
startAngle = Angle(radians: newValue.first)
endAngle = Angle(radians: newValue.second)
}
}
func path(in rect: CGRect) -> Path {
var path = Path()
let center = CGPoint(x: rect.midX, y: rect.midY)
let radius = min(rect.width, rect.height) / 2
path.move(to: center)
path.addArc(center: center, radius: radius,
startAngle: startAngle, endAngle: endAngle,
clockwise: false)
path.closeSubpath()
return path
}
}
iOS 26 之後 (使用 @Animatable)
// 新方式 - 只需加上 @Animatable
@Animatable
struct PieSlice: Shape {
var startAngle: Angle
var endAngle: Angle
func path(in rect: CGRect) -> Path {
var path = Path()
let center = CGPoint(x: rect.midX, y: rect.midY)
let radius = min(rect.width, rect.height) / 2
path.move(to: center)
path.addArc(center: center, radius: radius,
startAngle: startAngle, endAngle: endAngle,
clockwise: false)
path.closeSubpath()
return path
}
}
@AnimatableIgnored
從動畫中排除屬性:
@Animatable
struct CustomShape: Shape {
var animatedValue: CGFloat
@AnimatableIgnored var staticConfiguration: Bool // 不會被動畫化
func path(in rect: CGRect) -> Path {
// 使用兩個值,但只有 animatedValue 會產生動畫
}
}
支援的動畫型別
@Animatable 巨集會自動處理:
CGFloat,Double,FloatAngleCGSize,CGPoint,CGRectUnitPointColor(元件插值)- 符合
VectorArithmetic的自訂型別
多屬性複雜範例
@Animatable
struct MorphingShape: Shape {
var cornerRadius: CGFloat
var insetAmount: CGFloat
var rotation: Angle
@AnimatableIgnored var fillColor: Color
func path(in rect: CGRect) -> Path {
let insetRect = rect.insetBy(dx: insetAmount, dy: insetAmount)
var path = Path(roundedRect: insetRect, cornerRadius: cornerRadius)
let transform = CGAffineTransform(rotationAngle: rotation.radians)
return path.applying(transform)
}
}
// 使用方式
struct MorphingView: View {
@State private var isExpanded = false
var body: some View {
MorphingShape(
cornerRadius: isExpanded ? 50 : 10,
insetAmount: isExpanded ? 20 : 50,
rotation: isExpanded ? .degrees(45) : .zero,
fillColor: .blue
)
.fill(.blue)
.frame(width: 200, height: 200)
.onTapGesture {
withAnimation(.spring(duration: 0.6, bounce: 0.3)) {
isExpanded.toggle()
}
}
}
}
withAnimation
基本用法
struct ContentView: View {
@State private var isExpanded = false
var body: some View {
VStack {
Rectangle()
.frame(width: isExpanded ? 200 : 100,
height: isExpanded ? 200 : 100)
Button("切換") {
withAnimation {
isExpanded.toggle()
}
}
}
}
}
搭配動畫類型
withAnimation(.spring(duration: 0.5, bounce: 0.3)) {
isExpanded.toggle()
}
withAnimation(.easeInOut(duration: 0.3)) {
opacity = 1.0
}
withAnimation(.linear(duration: 1.0)) {
progress = 1.0
}
完成處理 (iOS 17+)
withAnimation(.easeInOut(duration: 0.5)) {
showDetails = true
} completion: {
// 動畫完成時呼叫
print("動畫結束")
fetchMoreData()
}
不同時間的巢狀動畫
Button(\