๐Ÿ…ฟ๏ธAnimatable

SwiftUI โŸฉ Animations โŸฉ Animatable โŸฉ

Animatable describes how to animate a view with respect to some change in the view's data. Use Animatable when you are unable to achieve the animation you want with animation(_:) or withAnimation(_:_:).

By conforming to Animatable, you are able to effectively decouple the animation of your view from the concept of duration, as you give SwiftUI the ability to interpolate arbitrarily between two different values for animatableData. This is also the reason why AnimatableData must conform to VectorArithmetic, which provides the runtime means to add, subtract and scale the animated values as necessary to generate data points for each frame of the animation over an arbitrary time interval.

  • 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 di๏ฌ€erent values during an animation, we can add log statements to the setter of animatableData or the body method of the animatable modi๏ฌer.

related protocol: `Animatable`

  • `animatableData`: computed property (with default implementation by doing nothing)

  • `Shape` conforms to `Animatable`

  • `AnimatablePair<First, Second>`: animate ove 2 parameters

  • `Animatable`: Angle, CGPoint, CGRect, CGSize, EdgeInsets, StrokeStyle, UnitPoint.

  • `VectorArithmetic`: AnimatablePair, CGFloat, Double, EmptyAnimatableData, Float.

  • When you use the animation(_:) modifier on an equatable view, SwiftUI animates any changes to animatable properties of the view. A viewโ€™s color, opacity, rotation, size, and other properties are all animatable.

  • When the view isnโ€™t equatable, you can use the animation(_:value:) modifier to start animations when the specified value changes.

Last updated