A representation of a spring's motion.
Use this type to convert between different representations of spring
parameters:
let spring = Spring(duration: 0.5, bounce: 0.3)
let (mass, stiffness, damping) = (spring.mass, spring.stiffness, spring.damping)
// (1.0, 157.9, 17.6)
let spring2 = Spring(mass: 1, stiffness: 100, damping: 10)
let (duration, bounce) = (spring2.duration, spring2.bounce)
// (0.63, 0.5)
You can also use it to query for a spring's position and its other properties for
a given set of inputs:
func unitPosition(time: TimeInterval) -> Double {
let spring = Spring(duration: 0.5, bounce: 0.3)
return spring.position(target: 1.0, time: time)
}
Members (27)
get · iOS 17.0+
dampingRatio
TypeScript
get dampingRatio(): number
Swift declaration
var dampingRatio: Double { get }
The amount of drag applied, as a fraction of the amount needed to
produce critical damping.
When `dampingRatio` is 1, the spring will smoothly decelerate to its
final position without oscillating. Damping ratios less than 1 will
oscillate more and more before coming to a complete stop.
init · iOS 17.0+
init
TypeScript
static initSettlingDurationDampingRatioEpsilon(settlingDuration: number, dampingRatio: number, epsilon?: number): Spring
Creates a spring with the specified duration and damping ratio.
- Parameters:
- settlingDuration: The approximate time it will take for the spring
to come to rest.
- dampingRatio: The amount of drag applied as a fraction of the amount
needed to produce critical damping.
- epsilon: The threshhold for how small all subsequent values need to
be before the spring is considered to have settled.
method · iOS 17.0+
settlingDuration
TypeScript
settlingDurationTargetInitialVelocityEpsilon(target: number, initialVelocity: number, epsilon: number): number
Swift declaration
func settlingDuration<V>(target: V, initialVelocity: V = .zero, epsilon: Double) -> TimeInterval where V : VectorArithmetic
The estimated duration required for the spring system to be considered
at rest.
The epsilon value specifies the threshold for how small all subsequent
values need to be before the spring is considered to have settled.
method · iOS 17.0+
settlingDuration
TypeScript
settlingDurationFromValueToValueInitialVelocityEpsilon(fromValue: number, toValue: number, initialVelocity: number, epsilon: number): number
Swift declaration
func settlingDuration<V>(fromValue: V, toValue: V, initialVelocity: V, epsilon: Double) -> TimeInterval where V : Animatable
The estimated duration required for the spring system to be considered
at rest.
The epsilon value specifies the threshold for how small all subsequent
values need to be before the spring is considered to have settled.
get · iOS 17.0+
settlingDuration
TypeScript
get settlingDuration(): number
Swift declaration
var settlingDuration: TimeInterval { get }
The estimated duration required for the spring system to be considered
at rest.
This uses a `target` of 1.0, an `initialVelocity` of 0, and an `epsilon`
of 0.001.
init · iOS 17.0+
init
TypeScript
static initMassStiffnessDampingAllowOverDamping(mass: number, stiffness: number, damping: number, allowOverDamping?: boolean): Spring
Creates a spring with the specified mass, stiffness, and damping.
- Parameters:
- mass: Specifies that property of the object attached to the end of
the spring.
- stiffness: The corresponding spring coefficient.
- damping: Defines how the spring's motion should be damped due to the
forces of friction.
- allowOverDamping: A value of true specifies that over-damping
should be allowed when appropriate based on the other inputs, and a
value of false specifies that such cases should instead be treated as
critically damped.
get · iOS 17.0+
mass
TypeScript
get mass(): number
Swift declaration
var mass: Double { get }
The mass of the object attached to the end of the spring.
The default mass is 1. Increasing this value will increase the spring's
effect: the attached object will be subject to more oscillations and
greater overshoot, resulting in an increased settling duration.
Decreasing the mass will reduce the spring effect: there will be fewer
oscillations and a reduced overshoot, resulting in a decreased
settling duration.
method · iOS 17.0+
force
TypeScript
forceTargetPositionVelocity(target: number, position: number, velocity: number): number
Swift declaration
func force<V>(target: V, position: V, velocity: V) -> V where V : VectorArithmetic
Calculates the force upon the spring given a current position, target,
and velocity amount of change.
This value is in units of the vector type per second squared.
method · iOS 17.0+
force
TypeScript
forceFromValueToValuePositionVelocity(fromValue: number, toValue: number, position: number, velocity: number): number
Swift declaration
func force<V>(fromValue: V, toValue: V, position: V, velocity: V) -> V where V : Animatable
Calculates the force upon the spring given a current position,
velocity, and divisor from the starting and end values for the spring to travel.
This value is in units of the vector type per second squared.
method · iOS 17.0+
value
TypeScript
valueTargetInitialVelocityTime(target: number, initialVelocity: number, time: number): number
Swift declaration
func value<V>(target: V, initialVelocity: V = .zero, time: TimeInterval) -> V where V : VectorArithmetic
Calculates the value of the spring at a given time given a target
amount of change.
method · iOS 17.0+
value
TypeScript
valueFromValueToValueInitialVelocityTime(fromValue: number, toValue: number, initialVelocity: number, time: number): number
Swift declaration
func value<V>(fromValue: V, toValue: V, initialVelocity: V, time: TimeInterval) -> V where V : Animatable
Calculates the value of the spring at a given time for a starting
and ending value for the spring to travel.
get · iOS 17.0+
bounce
TypeScript
get bounce(): number
Swift declaration
var bounce: Double { get }
How bouncy the spring is.
A value of 0 indicates no bounces (a critically damped spring), positive
values indicate increasing amounts of bounciness up to a maximum of 1.0
(corresponding to undamped oscillation), and negative values indicate
overdamped springs with a minimum value of -1.0.
static · iOS 17.0+
bouncy
TypeScript
static bouncyDurationExtraBounce(duration?: number, extraBounce?: number): Spring
A spring with a predefined duration and higher amount of bounce that
can be tuned.
- Parameters:
- duration: The perceptual duration, which defines the pace of the
spring. This is approximately equal to the settling duration, but
for very bouncy springs, will be the duration of the period of
oscillation for the spring.
- extraBounce: How much additional bounce should be added to the base
bounce of 0.3.
get · iOS 17.0+
bouncy
TypeScript
static get bouncy(): Spring
Swift declaration
@export(implementation) static var bouncy: Spring { get }
A spring with a predefined duration and higher amount of bounce.
static · iOS 17.0+
smooth
TypeScript
static smoothDurationExtraBounce(duration?: number, extraBounce?: number): Spring
A smooth spring with a predefined duration and no bounce that can be
tuned.
- Parameters:
- duration: The perceptual duration, which defines the pace of the
spring. This is approximately equal to the settling duration, but
for very bouncy springs, will be the duration of the period of
oscillation for the spring.
- extraBounce: How much additional bounce should be added to the base
bounce of 0.
get · iOS 17.0+
smooth
TypeScript
static get smooth(): Spring
Swift declaration
@export(implementation) static var smooth: Spring { get }
A smooth spring with a predefined duration and no bounce.
static · iOS 17.0+
snappy
TypeScript
static snappyDurationExtraBounce(duration?: number, extraBounce?: number): Spring
A spring with a predefined duration and small amount of bounce that
feels more snappy and can be tuned.
- Parameters:
- duration: The perceptual duration, which defines the pace of the
spring. This is approximately equal to the settling duration, but
for very bouncy springs, will be the duration of the period of
oscillation for the spring.
- extraBounce: How much additional bounciness should be added to the
base bounce of 0.15.
get · iOS 17.0+
snappy
TypeScript
static get snappy(): Spring
Swift declaration
@export(implementation) static var snappy: Spring { get }
A spring with a predefined duration and small amount of bounce that
feels more snappy.
func update<V>(value: inout V, velocity: inout V, target: V, deltaTime: TimeInterval) where V : VectorArithmetic
Updates the current value and velocity of a spring.
- Parameters:
- value: The current value of the spring.
- velocity: The current velocity of the spring.
- target: The target that `value` is moving towards.
- deltaTime: The amount of time that has passed since the spring was
at the position specified by `value`.
Omit the output parameters from the call. The result contains $ret and $out, with value, velocity inside $out.
get · iOS 17.0+
damping
TypeScript
get damping(): number
Swift declaration
var damping: Double { get }
Defines how the spring’s motion should be damped due to the forces of
friction.
Reducing this value reduces the energy loss with each oscillation: the
spring will overshoot its destination. Increasing the value increases
the energy loss with each duration: there will be fewer and smaller
oscillations.
init · iOS 17.0+
init
TypeScript
static initDurationBounce(duration?: number, bounce?: number): Spring
Creates a spring with the specified duration and bounce.
- Parameters:
- duration: Defines the pace of the spring. This is approximately
equal to the settling duration, but for springs with very large
bounce values, will be the duration of the period of oscillation
for the spring.
- bounce: How bouncy the spring should be. A value of 0 indicates
no bounces (a critically damped spring), positive values indicate
increasing amounts of bounciness up to a maximum of 1.0
(corresponding to undamped oscillation), and negative values
indicate overdamped springs with a minimum value of -1.0.
get · iOS 17.0+
duration
TypeScript
get duration(): number
Swift declaration
var duration: TimeInterval { get }
The perceptual duration, which defines the pace of the spring.
init · iOS 17.0+
init
TypeScript
static initResponseDampingRatio(response: number, dampingRatio: number): Spring
Swift declaration
init(response: Double, dampingRatio: Double)
Creates a spring with the specified response and damping ratio.
- Parameters:
- response: Defines the stiffness of the spring as an approximate
duration in seconds.
- dampingRatio: Defines the amount of drag applied as a fraction the
amount needed to produce critical damping.
get · iOS 17.0+
response
TypeScript
get response(): number
Swift declaration
var response: Double { get }
The stiffness of the spring, defined as an approximate duration in
seconds.
method · iOS 17.0+
velocity
TypeScript
velocityTargetInitialVelocityTime(target: number, initialVelocity: number, time: number): number
Swift declaration
func velocity<V>(target: V, initialVelocity: V = .zero, time: TimeInterval) -> V where V : VectorArithmetic
Calculates the velocity of the spring at a given time given a target
amount of change.
method · iOS 17.0+
velocity
TypeScript
velocityFromValueToValueInitialVelocityTime(fromValue: number, toValue: number, initialVelocity: number, time: number): number
Swift declaration
func velocity<V>(fromValue: V, toValue: V, initialVelocity: V, time: TimeInterval) -> V where V : Animatable
Calculates the velocity of the spring at a given time given a starting
and ending value for the spring to travel.
get · iOS 17.0+
stiffness
TypeScript
get stiffness(): number
Swift declaration
var stiffness: Double { get }
The spring stiffness coefficient.
Increasing the stiffness reduces the number of oscillations and will
reduce the settling duration. Decreasing the stiffness increases the
number of oscillations and will increase the settling duration.