Animatable
public final class Animatable<Value: VectorConvertible>
Instances of Animatable
wrap, and manage animated change to, a value
conforming to VectorConvertible
.
Using this class to represent a value is often cleaner than setting
up and managing animations independently, as Animatable
conveniently
channels all changes to the value into the changed
event. For example:
class Foo {
let size: Animatable<CGSize>
(...)
init() {
size.changed.observe { [weak self] (val) in
self?.doSomethingWithSize(val)
}
}
}
let f = Foo()
f.size.animateTo(CGSize(width: 200.0, height: 200.0))
-
finished
will be true if the animation finished uninterrupted, or false if it was cancelled.Declaration
Swift
public typealias Completion = (_ finished: Bool) -> Void
-
Returns
true
if an animation is in progress.Declaration
Swift
public var animating: Bool
-
The current value of this
Animatable
.Setting this property will cancel any animation that is in progress, and this
Animatable
will assume the new value immediately.Declaration
Swift
public var value: Value
-
The current velocity reported by the in-flight animation, if any. If no animation is in progress, the returned value will be equivalent to
T.Vector.zero
Declaration
Swift
public var velocity: Value
-
Creates an
Animatable
of T initialized to an initial value.Declaration
Swift
public required init(value: Value)
Parameters
value
The initial value of this animatable.
-
Runs the given animation until is either completes or is removed (by starting another animation or by directly setting the value).
Declaration
Swift
public func animate<A: ValueAnimationType>(_ animation: A, completion: Completion? = nil) where A.Value == Value
Parameters
animation
The animation to be run.
completion
An optional closure that will be called when this animation has completed. Its only argument is a
Boolean
, which will betrue
if the animation completed uninterrupted, orfalse
if it was removed for any other reason. -
Cancels an in-flight animation, if present.
Declaration
Swift
public func cancelAnimation()
-
Animates to the specified value, using a default duration and timing function.
Declaration
Swift
public func animateTo(_ to: Value, completion: Completion? = nil)
Parameters
to
The value to animate to.
completion
An optional closure that will be called when the animation completes.
-
Animates to the specified value.
Declaration
Swift
public func animateTo(_ to: Value, duration: Double, timingFunction: TimingFunctionType, completion: Completion? = nil)
Parameters
to
The value to animate to.
duration
The duration of the animation.
timingFunction
The timing (easing) function to use.
completion
An optional closure that will be called when the animation completes.
-
Adds a decay animation, starting from the current value and velocity.
Declaration
Swift
public func decay(_ drag: Scalar, threshold: Scalar, completion: Completion? = nil)
Parameters
drag
The amount of drag that will slow down the velocity.
threshold
The settling threshold that determines how close the velocity must be to
0
before the simulation is allowed to settle.completion
An optional closure that will be called at the end of the animation.
-
Adds a decay animation, starting from the current value.
Declaration
Swift
public func decay(_ velocity: Value, drag: Scalar, threshold: Scalar, completion: Completion? = nil)
Parameters
velocity
The initial velocity at time
0
.drag
The amount of drag that will slow down the velocity.
threshold
The settling threshold that determines how close the velocity must be to
0
before the simulation is allowed to settle.completion
An optional closure that will be called at the end of the animation.
-
Animates to the given value using a spring function.
Declaration
Swift
public func springTo(_ to: Value, initialVelocity: Value? = nil, configuration: SpringConfiguration = SpringConfiguration(), completion: Completion? = nil)
Parameters
to
The value to animate to.
initialVelocity
An optional velocity to use at time
0
. If no velocity is given, the current velocity of theAnimatable
instance will be used (if another animation is in progress).configuration
A spring configuration instance to use.
completion
A closure that will be called at the end of the animation.