🅿️GeometryEffect
When an animatable modifier animates something that can be expressed as an affine transformation (2D/3D), we can also use GeometryEffect instead of Animatable Modifiers.
struct ShakeEffect: GeometryEffect {
var times: CGFloat = 0
let amplitude: CGFloat = 10
// ⭐️ 🅿️ Animatable
var animatableData: CGFloat {
get { times }
set { times = newValue }
}
// ⭐️ 🅿️ GeometryEffect
func effectValue(size: CGSize) -> ProjectionTransform {
ProjectionTransform(CGAffineTransform(
translationX: sin(times * .pi * 2) * amplitude, y: 0
))
}
}
SwiftUI ⟩
Drawing and Graphics ⟩ ProjectionTransform (struct) - 3x3 matrix.
Animations ⟩ GeometryEffect (protocol)
.effectValue(size:) - required instance method.

SwiftUI Lab ⟩ Advanced SwiftUI Animations ⟩
Last updated
Was this helpful?