๐Ÿ…ฟ๏ธAnimatable

SwiftUI โŸฉ animations โŸฉ Animatable โŸฉ

// โญ๏ธ ไธ€็ถญ็š„ `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

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.

related protocol: `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

Was this helpful?