React-Native Animation
Syntax
React Native provides the Animated API for creating animations. The Animated library can be used to create animations for properties such as opacity, position, and scale.
import { Animated } from 'react-native';
const fadeAnim = new Animated.Value(0);
Animated.timing(
fadeAnim,
{
toValue: 1,
duration: 1000,
useNativeDriver: true
}
).start();
Example
import React, { useRef } from 'react';
import { Animated, View, Button } from 'react-native';
export default function Example() {
const fadeAnim = useRef(new Animated.Value(0)).current;
const fadeIn = () => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: 1000,
useNativeDriver: true,
}).start();
};
const fadeOut = () => {
Animated.timing(fadeAnim, {
toValue: 0,
duration: 1000,
useNativeDriver: true,
}).start();
};
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Animated.View
style={{
opacity: fadeAnim,
width: 250,
height: 50,
backgroundColor: 'yellow',
alignItems: 'center',
justifyContent: 'center',
}}
>
<Button title='Fade In' onPress={fadeIn} />
<Button title='Fade Out' onPress={fadeOut} />
</Animated.View>
</View>
);
}
Output
The above example will render a yellow view containing two buttons. Pressing the "Fade In" button will gradually increase the opacity of the view, while pressing the "Fade Out" button will gradually decrease the opacity of the view.
Explanation
The Animated API is a powerful tool for creating a variety of animations in React Native. The API works by creating an Animated.Value
object with an initial value (e.g. new Animated.Value(0)
), and then specifying how that value should change over time using one of several animation classes.
In the above example, we use the Animated.timing
method to gradually change the opacity of a view from 0 to 1 (or vice versa). We specify the toValue
we want to animate to, the duration
of the animation in milliseconds, and the useNativeDriver
boolean flag which allows the animation to be performed natively on the device.
Use
Animations are an important part of many mobile applications, and the Animated API in React Native provides a powerful way to create smooth, performant animations in your app.
Some common animation classes offered by the Animated API include:
Animated.timing
: Changes a value over timeAnimated.spring
: Simulates spring physicsAnimated.decay
: Simulates movement with frictionAnimated.sequence
: Runs several animations in sequenceAnimated.parallel
: Runs several animations in parallel
Important Points
- Animations should be used sparingly to avoid making your app feel too "busy" or overwhelming.
- Animations can be resource-intensive, so be careful not to use too many or too complex animations in your app.
- The useNativeDriver flag is required for animations on certain properties, such as position and scale, in order to obtain optimal performance.
Summary
The Animated API in React Native provides a powerful tool for creating smooth, dynamic animations in mobile applications. By using the Animated.timing
method with the useNativeDriver
flag, you can create performant animations that work seamlessly across a variety of devices.