🅿️Animatable
SwiftUI ⟩ animations ⟩ Animatable ⟩
Animatable describes how to animate a view with respect to some change in the view's data.
animatableData
: required computed property (with default implementation by doing nothing)Shape conforms to Animatable
Use Animatable when you are unable to achieve the animation you want with animation(_:) or withAnimation(_:_:).
// ⭐️ 一維的 `animatableData`
var animatableData: CGFloat {
get { return sides }
set { sides = newValue } // ⭐️ set new animation value
}
// ⭐️ 二維的 `animatableData` (AnimatablePair)
var animatableData: AnimatablePair<CGFloat, CGFloat> {
get { AnimatablePair(sides, scale) }
set { // ⭐️ set new animation value
sides = newValue.first
scale = newValue.second
}
}
👉 see: Polygon
To expose two properties as animatable, wrap them in an AnimatablePair.
nest AnimatablePairs inside each other to support any number of properties.
To examine how SwiftUI interpolates between different values during an animation, we can add log statements to the setter of animatableData or the body method of the animatable modifier.
Last updated
Was this helpful?