Animated

Animations are an important part of modern UX, and the Animated library is designed to make them fluid, powerful, and easy to build and maintain.

The simplest workflow is to create an Animated.Value, hook it up to one or more style attributes of an animated component, and then drive updates either via animations, such as Animated.timing, or by hooking into gestures like panning or scrolling via Animated.event. Animated.Value can also bind to props other than style, and can be interpolated as well. Here is a basic example of a container view that will fade in when it's mounted:

Note that only animatable components can be animated. View, Text, and Image are already provided, and you can create custom ones with createAnimatedComponent. 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.

Animations are heavily configurable. Custom and pre-defined easing functions, delays, durations, decay factors, spring constants, and more can all be tweaked depending on the type of animation.

A single Animated.Value can drive any number of properties, and each property can be run through an interpolation first. An interpolation maps input ranges to output ranges, typically using a linear interpolation but also supports easing functions. By default, it will extrapolate the curve beyond the ranges given, but you can also have it clamp the output value.

For example, you may want to think about your Animated.Value as going from 0 to 1, but animate the position from 150px to 0px and the opacity from 0 to

This can easily be done by modifying style in the example above like so:

Animations can also be combined in complex ways using composition functions such as sequence and parallel, and can also be chained together simply by setting the toValue of one animation to be another Animated.Value.

Animated.ValueXY is handy for 2D animations, like panning, and there are other helpful additions like setOffset and getLayout to aid with typical interaction patterns, like drag-and-drop.

Note that Animated is designed to be fully serializable so that animations can be run in a high performance way, independent of the normal JavaScript event loop. This does influence the API, so keep that in mind when it seems a little trickier to do something compared to a fully synchronous system. Checkout Animated.Value.addListener as a way to work around some of these limitations, but use it sparingly since it might have performance implications in the future.

createAnimatedComponent()

staticcreateAnimatedComponent(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.

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. Example: