SpringInterpolatable is a protocol that enables custom types to be animated with spring-based physics in Wave. Types conforming to this protocol can calculate their values over time based on spring dynamics.
Protocol definition
Associated types
The value type that will be interpolated. Must itself conform to
SpringInterpolatable.The type used to represent velocity during spring calculations. Must conform to
VelocityProviding.Required properties
Returns a value rounded to the nearest pixel boundary, taking into account the device’s display scale. This prevents subpixel rendering artifacts during animation.For
CGFloat, this is calculated as floor(self * scale) / scale where scale is the screen’s scale factor.Required methods
Calculates the next value and velocity for a spring animation step.Parameters:
spring: The spring configuration defining the animation physicsvalue: The current valuetarget: The target value to animate towardsvelocity: The current velocitydt: The time interval (delta time) for this step
- Calculate displacement:
value - target - Calculate spring force:
-spring.stiffness * displacement - Calculate damping force:
spring.dampingCoefficient * velocity - Calculate total force:
springForce - dampingForce - Calculate acceleration:
force / spring.mass - Update velocity:
velocity + (acceleration * dt) - Update value:
value + (newVelocity * dt)
Built-in conformances
Wave providesSpringInterpolatable conformance for the following Core Graphics types:
CGFloat
The fundamental building block for spring animations. All other types build uponCGFloat’s implementation.
CGPoint
Animates 2D points by independently animatingx and y components.
CGSize
Animates sizes by independently animatingwidth and height components.
CGRect
Animates rectangles by independently animating origin and size components.VelocityProviding protocol
Types used asVelocityType must conform to the VelocityProviding protocol:
zero value representing no velocity, which is used as the initial velocity for animations.
Implementing custom conformances
To make a custom type animatable with Wave, implementSpringInterpolatable:
See also
- Spring - Configure spring physics parameters
- AnimationMode - Control whether animations are animated or immediate
- Wave.animate - Perform spring-based animations