GeometryEffect
When an animatable modi๏ฌer animates something that can be expressed as an a๏ฌne 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?