SpringAnimator provides direct control over spring animations, allowing you to manually manage animation state, update values, and respond to animation events.
Overview
SpringAnimator is a generic class that can animate any type conforming to SpringInterpolatable. While most developers will use the high-level Wave.animate() API, SpringAnimator is useful when you need:
- Fine-grained control over animation lifecycle
- Custom animation types
- Manual animation updates
- Dynamic retargeting during animation
Type parameters
SpringAnimator is a generic class:
T is the type being animated (e.g., CGFloat, CGPoint, CGRect, etc.).
Initializer
init(spring:value:target:)
Creates a new animation with a givenSpring, and optionally, an initial and target value.
The spring model that determines the animation’s motion.
The initial, starting value of the animation. While optional in the initializer, it must be set to a non-nil value before the animation can start.
The target value of the animation. While optional in the initializer, it must be set to a non-nil value before the animation can start.
While
value and target are optional in the initializer, they must be set to non-nil values before the animation can start.Properties
id
A unique identifier for the animation.state
The execution state of the animation (inactive, running, or ended).
.inactive: The animation is not currently running, but is ready.running: The animation is currently active and executing.ended: The animation has just stopped, and will be reset to theinactivestate
spring
The spring model that determines the animation’s motion.value
The current value of the animation. This value will change as the animation executes.value needs to be set to a non-nil value before the animation can start.target
The current target value of the animation.velocity
The current velocity of the animation.valueChanged
The callback block to call when the animation’svalue changes as it executes. Use the currentValue to drive your application’s animations.
completion
The completion block to call when the animation either finishes, or “re-targets” to a new target value.mode
The animation’s mode. If set to.nonAnimated, the animation will snap to the target value when run.
.animated
integralizeValues
Whether the values returned invalueChanged should be integralized to the screen’s pixel boundaries. This helps prevent drawing frames between pixels, causing aliasing issues.
false
Enabling
integralizeValues effectively quantizes value, so don’t use this for values that are supposed to be continuous.settlingTime
How long the animation will take to complete, based off itsspring property.
This is useful for debugging purposes only. Do not use
settlingTime to determine the animation’s progress.Methods
start(afterDelay:)
Starts the animation (if not already running) with an optional delay.The amount of time (measured in seconds) to wait before starting the animation.
stop(immediately:)
Stops the animation at the current value.If
true, the animation stops immediately at the current value. If false, the animation will animate to the current value (sets target to the current value).Event enumeration
SpringAnimator.Event represents animation lifecycle events:
finished(at:)
Indicates the animation has fully completed.retargeted(from:to:)
Indicates that the animation’starget value was changed in-flight (i.e. while the animation was running).
The previous
target value of the animation.The new
target value of the animation.