Animations et Transitions SwiftUI
Guide complet des animations SwiftUI, du nouveau macro @Animatable (iOS 26), des transitions et des bonnes pratiques de conception de mouvements pour le développement iOS moderne.
Prérequis
- iOS 17+ pour PhaseAnimator/KeyframeAnimator
- iOS 26+ pour le macro
@Animatable - Xcode 26+
Macro @Animatable (iOS 26 - NOUVEAU !)
La révolution des animations personnalisées
iOS 26 introduit le macro @Animatable, éliminant le code rébarbatif (boilerplate) nécessaire auparavant pour animer des formes et des vues personnalisées.
Avant iOS 26 (Approche manuelle)
// ANCIENNE MÉTHODE - Beaucoup de code rébarbatif
struct PieSlice: Shape {
var startAngle: Angle
var endAngle: Angle
// Implémentation manuelle requise de 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
}
}
Après iOS 26 (Avec @Animatable)
// NOUVELLE MÉTHODE - Ajoutez simplement @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
Exclure des propriétés de l'animation :
@Animatable
struct CustomShape: Shape {
var animatedValue: CGFloat
@AnimatableIgnored var staticConfiguration: Bool // Non animé
func path(in rect: CGRect) -> Path {
// Utilise les deux valeurs, mais seul animatedValue sera animé
}
}
Types pris en charge pour l'animation
Le macro @Animatable gère automatiquement :
CGFloat,Double,FloatAngleCGSize,CGPoint,CGRectUnitPointColor(interpolation de composants)- Types personnalisés conformes à
VectorArithmetic
Exemple complexe avec plusieurs propriétés
@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)
}
}
// Utilisation
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
Utilisation de base
struct ContentView: View {
@State private var isExpanded = false
var body: some View {
VStack {
Rectangle()
.frame(width: isExpanded ? 200 : 100,
height: isExpanded ? 200 : 100)
Button("Basculer") {
withAnimation {
isExpanded.toggle()
}
}
}
}
}
Avec type d'animation
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
}
Bloc de fin (iOS 17+)
withAnimation(.easeInOut(duration: 0.5)) {
showDetails = true
} completion: {
// Appelé lorsque l'animation se termine
print(\