Cours React Native · Module 10: Animations
react-native-reanimated - Animations avancées
Dans cette leçon4
Reanimated est une bibliothèque d'animation plus puissante !
Installation
npx expo install react-native-reanimatedConfiguration de babel.config.js
module.exports = {
presets: ['babel-preset-expo'],
plugins: ['react-native-reanimated/plugin'], // doit être en dernier !
};Les bases de Reanimated
import Animated, {
useSharedValue,
useAnimatedStyle,
withSpring,
withTiming,
} from 'react-native-reanimated';
function SpringBox() {
const offset = useSharedValue(0);
const animatedStyles = useAnimatedStyle(() => ({
transform: [{ translateX: offset.value }],
}));
const moveRight = () => {
offset.value = withSpring(200);
};
const moveLeft = () => {
offset.value = withSpring(0);
};
return (
<View>
<Animated.View style={[styles.box, animatedStyles]} />
<Pressable onPress={moveRight}><Text>Droite</Text></Pressable>
<Pressable onPress={moveLeft}><Text>Gauche</Text></Pressable>
</View>
);
}Les gestes avec Gesture Handler
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import Animated, { useSharedValue, useAnimatedStyle, withSpring } from 'react-native-reanimated';
function DraggableBox() {
const translateX = useSharedValue(0);
const translateY = useSharedValue(0);
const panGesture = Gesture.Pan()
.onUpdate((event) => {
translateX.value = event.translationX;
translateY.value = event.translationY;
})
.onEnd(() => {
translateX.value = withSpring(0);
translateY.value = withSpring(0);
});
const animatedStyle = useAnimatedStyle(() => ({
transform: [
{ translateX: translateX.value },
{ translateY: translateY.value },
],
}));
return (
<GestureDetector gesture={panGesture}>
<Animated.View style={[styles.box, animatedStyle]} />
</GestureDetector>
);
}Code de cette leçon : App.tsx
import React from 'react';
import { View, Text, Pressable, StyleSheet } from 'react-native';
import Animated, {
useSharedValue,
useAnimatedStyle,
withSpring,
} from 'react-native-reanimated';
export default function App() {
// TODO : créer une valeur partagée pour le décalage horizontal (départ : 0)
// const offset = useSharedValue(0);
// TODO : construire un style animé qui déplace la vue de offset.value :
// const animatedStyles = useAnimatedStyle(() => ({
// transform: [{ translateX: offset.value }],
// }));
const moveRight = () => {
// TODO : assigner offset.value = withSpring(120);
};
const moveLeft = () => {
// TODO : assigner offset.value = withSpring(0);
};
return (
<View style={styles.container}>
{/* TODO : attacher animatedStyles au style de la box :
style={[styles.box, animatedStyles]} */}
<Animated.View style={styles.box} />
<View style={styles.row}>
<Pressable style={styles.button} onPress={moveLeft}>
<Text style={styles.buttonText}>Gauche</Text>
</Pressable>
<Pressable style={styles.button} onPress={moveRight}>
<Text style={styles.buttonText}>Droite</Text>
</Pressable>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#1a5276',
},
box: {
width: 90,
height: 90,
borderRadius: 12,
backgroundColor: '#00d4ff',
marginBottom: 32,
},
row: {
flexDirection: 'row',
gap: 12,
},
button: {
paddingVertical: 12,
paddingHorizontal: 24,
borderRadius: 8,
backgroundColor: '#2980b9',
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
});Entrées de la documentation utilisées dans cette leçon
Tu as repéré une erreur dans cette leçon ?
Teste tes connaissances
Réponds aux questions de cette leçon. Choisis une réponse pour voir tout de suite si elle est correcte.
1. Comment installe-t-on react-native-reanimated ?
2. Où doit se trouver le plugin reanimated dans babel.config.js ?
Voici 2 des 9 questions de cette leçon. Réponds aux autres dans le jeu.
Tâches pratiques dans le jeu
- Tri horizontal
Ordonne useAnimatedStyle :
- Clique dans l'ordre
Clique sur les éléments de withTiming :
- Éditeur de code
Complète le code en utilisant withSpring.
Exercices après cette leçon
- QuestionInstaller Reanimated
- Questionbabel.config.js
- QuestionuseSharedValue
- QuestionModifier .value
- Remets dans l'ordreuseAnimatedStyle
- QuestionwithSpring
- Clique dans l'ordreConfiguration de withTiming
- Exercice de codeAnimation spring
- QuestionGesture.Pan
- QuestionGestureDetector
- QuestiononUpdate
- QuestionrunOnJS