🅿️Animatable

SwiftUIanimationsAnimatable

circle-check
// ⭐️ 一維的 `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

triangle-exclamation
circle-info

By conforming to Animatablearrow-up-right, 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 animatableDataarrow-up-right. This is also the reason why AnimatableDataarrow-up-right must conform to VectorArithmeticarrow-up-right, 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.

circle-check
circle-check
circle-info

related protocol: `Animatable`

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

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

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

circle-info
  • 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

Was this helpful?