Animated

The Animated library is designed to make animations fluid, powerful, and easy to build and maintain. Animated focuses on declarative relationships between inputs and outputs, with configurable transforms in between, and simple start/stop methods to control time-based animation execution.

The simplest workflow for creating an animation is to to create an Animated.Value, hook it up to one or more style attributes of an animated component, and then drive updates via animations using Animated.timing():

In most cases, you will be using timing(). By default, it uses a symmetric easeInOut curve that conveys the gradual acceleration of an object to full speed and concludes by gradually decelerating to a stop.

Working with animations

Animations are started by calling start() on your animation. start() takes a completion callback that will be called when the animation is done. If the animation finished running normally, the completion callback will be invoked with {finished: true}. If the animation is done because stop() was called on it before it could finish (e.g. because it was interrupted by a gesture or another animation), then it will receive {finished: false}.

Using the native driver

By using the native driver, we send everything about the animation to native before starting the animation, allowing native code to perform the animation on the UI thread without having to go through the bridge on every frame. Once the animation has started, the JS thread can be blocked without affecting the animation.

You can use the native driver by specifying useNativeDriver: true in your animation configuration. See the Animations guide to learn more.

Animatable components

Only animatable components can be animated. These special components do the magic of binding the animated values to the properties, and do targeted native updates to avoid the cost of the react render and reconciliation process on every frame. They also handle cleanup on unmount so they are safe by default.

Combining animated values

Interpolation

The interpolate() function allows input ranges to map to different output ranges. By default, it will extrapolate the curve beyond the ranges given, but you can also have it clamp the output value. It uses lineal interpolation by default but also supports easing functions.

Handling gestures and other events

Gestures, like panning or scrolling, and other events can map directly to animated values using Animated.event(). This is done with a structured map syntax so that values can be extracted from complex event objects. The first level is an array to allow mapping across multiple args, and that array contains nested objects.

spring()

Spring animation based on Rebound and Origami. Tracks velocity state to create fluid motions as the toValue updates, and can be chained together.

Config is an object that may have the following options:

friction: Controls "bounciness"/overshoot. Default 7.

tension: Controls speed. Default 40.

useNativeDriver: Uses the native driver when true. Default false.

add()

static add(a, b)

Creates a new Animated value composed from two Animated values added together.

divide()

static divide(a, b)

Creates a new Animated value composed by dividing the first Animated value by the second Animated value.

multiply()

static multiply(a, b)

Creates a new Animated value composed from two Animated values multiplied together.

modulo()

static modulo(a, modulus)

Creates a new Animated value that is the (non-negative) modulo of the provided Animated value

diffClamp()

static diffClamp(a, min, max)

Create a new Animated value that is limited between 2 values. It uses the difference between the last value so even if the value is far from the bounds it will start changing when the value starts getting closer again. (value = clamp(value + diff, min, max)).

This is useful with scroll events, for example, to show the navbar when scrolling up and to hide it when scrolling down.

delay()

static delay(time)

Starts an animation after the given delay.

sequence()

static sequence(animations)

Starts an array of animations in order, waiting for each to complete before starting the next. If the current running animation is stopped, no following animations will be started.

parallel()

static parallel(animations, config?)

Starts an array of animations all at the same time. By default, if one of the animations is stopped, they will all be stopped. You can override this with the stopTogether flag.

stagger()

static stagger(time, animations)

Array of animations may run in parallel (overlap), but are started in sequence with successive delays. Nice for doing trailing effects.

event()

static event(argMapping, config?)

Takes an array of mappings and extracts values from each arg accordingly, then calls setValue on the mapped outputs. e.g.

createAnimatedComponent()

static createAnimatedComponent(Component)

Make any React component Animatable. Used to create Animated.View, etc.

Properties

Classes

class AnimatedValue

Standard value for driving animations. One Animated.Value can drive multiple properties in a synchronized fashion, but can only be driven by one mechanism at a time. Using a new mechanism (e.g. starting a new animation, or calling setValue) will stop any previous ones.

Methods

constructor()

constructor(value);

setValue()

setValue(value);

Directly set the value. This will stop any animations running on the value and update all the bound properties.

setOffset()

setOffset(offset);

Sets an offset that is applied on top of whatever value is set, whether via setValue, an animation, or Animated.event. Useful for compensating things like the start of a pan gesture.

flattenOffset()

flattenOffset();

Merges the offset value into the base value and resets the offset to zero. The final output of the value is unchanged.

extractOffset()

extractOffset();

Sets the offset value to the base value, and resets the base value to zero. The final output of the value is unchanged.

addListener()

addListener(callback);

Adds an asynchronous listener to the value so you can observe updates from animations. This is useful because there is no way to synchronously read the value because it might be driven natively.

removeListener()

removeListener(id);

removeAllListeners()

removeAllListeners();

stopAnimation()

stopAnimation(callback?)

Stops any running animation or tracking. callback is invoked with the final value after stopping the animation, which is useful for updating state to match the animation position with layout.

interpolate()

interpolate(config);

Interpolates the value before updating the property, e.g. mapping 0-1 to 0-10.

animate()

animate(animation, callback);

Typically only used internally, but could be used by a custom Animation class.

stopTracking()

stopTracking();

Typically only used internally.

track()

track(tracking);

Typically only used internally.

class AnimatedValueXY

2D Value for driving 2D animations, such as pan gestures. Almost identical API to normal Animated.Value, but multiplexed. Contains two regular Animated.Values under the hood.