A timing animation follows a curve over a set number of milliseconds. A spring instead follows a physics model toward its target, and decay has no target at all, only a starting velocity that slows to a stop. For screens that move several values, Animated has functions that run several animations in order, together or with staggered starts.
Key terms
- Animated.spring: animates a value toward
toValuewith a spring physics model based on a damped harmonic oscillator. - Animated.decay: animates a value from an initial velocity down to zero, based on a decay coefficient.
- Animated.sequence: starts animations in order, each one waiting for the previous one to complete.
- Animated.parallel: starts a number of animations at the same time.
- Animated.stagger: starts animations in order with successive delays, so they can overlap.
Three ways to tune a spring
Animated.spring(value, config) returns a CompositeAnimation, like timing. It tracks velocity, so the motion stays fluid when toValue updates while the spring is moving, and springs can be chained.
The config describes the spring with exactly one of three option groups:
tensionandfriction, used when you set none of the others.tensioncontrols speed (default 40) andfrictioncontrols bounciness and overshoot (default 7). They are converted to stiffness and damping internally.bouncinessandspeed, with defaults of 8 and 12.stiffness,dampingandmass, with defaults of 100, 10 and 1. Each one must be greater than 0.
Mixing groups, for example friction with stiffness, fails an invariant whose message says only one group may be defined. Three more options work with any group. velocity is the initial velocity, 0 (at rest) by default. delay waits that many milliseconds first, 0 by default. overshootClamping: true clamps the spring so it doesn't bounce, and it is false by default.
Decay starts from a velocity
Animated.decay(value, config) requires a velocity and takes an optional deceleration. It computes the position from the start value, the velocity and the deceleration only. There is no toValue, so where the value stops depends on how fast it started and how quickly it slows. On the JS driver, the animation ends once the value moves less than 0.1 between frames.
deceleration defaults to 0.998, though the decay reference lists 0.997, so set it yourself when the stopping distance matters. A common source for the velocity is a gesture's release speed, such as the vx velocity that PanResponder reports, which the gestures section covers.
Running animations in order or together
The composition functions take your animations and start and stop them as needed, so you call start() once on the outer animation:
Animated.delay(ms)starts after the given time. It is a timing animation on a throwawayAnimated.Value(0), which makes it a pause you can place inside a sequence.Animated.sequence([...])waits for each child to complete before starting the next. If a child ends withfinished: false, the sequence calls its callback right away and starts none of the remaining children. Callingstart()again on a finished sequence replays it from the first animation.Animated.parallel([...])starts every child at once and calls its callback when all of them have ended, so it runs as long as its longest child. Stopping one child stops them all, unless you pass{stopTogether: false}as the second argument.Animated.stagger(ms, [...])runs in parallel, for each childi, a sequence ofdelay(ms * i)followed by that child. Childistartsms * imilliseconds after the stagger starts, and the stagger ends when the last running entry ends.Animated.loop(animation)resets its child and starts it again each time it reaches the end, forever unless you passiterations(default -1, infinite). It takes a single animation, even though the loop signature in the reference shows an array.
A level-up banner
The LevelUpBanner component below springs a badge in, then fades in three stars one after another. One Animated.sequence holds both steps, and the stars sit inside an Animated.stagger.
The spring sets friction: 4 in place of the default 7, which keeps it in the tension and friction group, so adding stiffness to that config would trip the one-group invariant. Once the spring completes, Animated.stagger(120, ...) starts the stars at 0, 120 and 240 ms. Each fade lasts 250 ms, so the stagger finishes 490 ms after it starts. The effect cleanup calls intro.stop(), and a stopped sequence starts none of the children that haven't run yet.
Code Examples
import {useEffect} from 'react';
import {Animated, StyleSheet, Text, View, useAnimatedValue} from 'react-native';
function fadeIn(star: Animated.Value) {
return Animated.timing(star, {toValue: 1, duration: 250, useNativeDriver: true});
}
export function LevelUpBanner({level}: {level: number}) {
const badgeScale = useAnimatedValue(0);
const star1 = useAnimatedValue(0);
const star2 = useAnimatedValue(0);
const star3 = useAnimatedValue(0);
useEffect(() => {
const intro = Animated.sequence([
Animated.spring(badgeScale, {toValue: 1, friction: 4, useNativeDriver: true}),
Animated.stagger(120, [fadeIn(star1), fadeIn(star2), fadeIn(star3)]),
]);
intro.start();
return () => intro.stop();
}, [badgeScale, star1, star2, star3]);
return (
<View style={styles.banner}>
<Animated.View style={{transform: [{scale: badgeScale}]}}>
<Text>Level {level}</Text>
</Animated.View>
<View style={styles.stars}>
{[star1, star2, star3].map((star, index) => (
<Animated.Text key={index} style={{opacity: star}}>
*
</Animated.Text>
))}
</View>
</View>
);
}
const styles = StyleSheet.create({
banner: {alignItems: 'center', padding: 24},
stars: {flexDirection: 'row', gap: 8},
});