Cours React Native · Module 10: Animations
expo-haptics - Vibrations
Ajoute un retour haptique aux interactions !
npx expo install expo-hapticsimport * as Haptics from 'expo-haptics';
// Différents types de vibration
const lightVibration = () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
const mediumVibration = () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
const heavyVibration = () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
// Notifications
const success = () => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
const warning = () => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
const error = () => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
// Sélection
const selection = () => Haptics.selectionAsync();
// Utilisation avec un geste
const panGesture = Gesture.Pan()
.onStart(() => {
runOnJS(Haptics.selectionAsync)();
});Projet : Carte animée
import Animated, { useSharedValue, useAnimatedStyle, withSpring, withTiming, interpolate } from 'react-native-reanimated';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import * as Haptics from 'expo-haptics';
function FlipCard({ front, back }: { front: ReactNode; back: ReactNode }) {
const rotation = useSharedValue(0);
const isFlipped = useSharedValue(false);
const flip = () => {
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
rotation.value = withSpring(isFlipped.value ? 0 : 180);
isFlipped.value = !isFlipped.value;
};
const frontStyle = useAnimatedStyle(() => ({
transform: [{ rotateY: `${rotation.value}deg` }],
opacity: interpolate(rotation.value, [0, 90, 180], [1, 0, 0]),
}));
const backStyle = useAnimatedStyle(() => ({
transform: [{ rotateY: `${rotation.value + 180}deg` }],
opacity: interpolate(rotation.value, [0, 90, 180], [0, 0, 1]),
}));
return (
<Pressable onPress={flip}>
<Animated.View style={[styles.card, frontStyle]}>{front}</Animated.View>
<Animated.View style={[styles.card, styles.cardBack, backStyle]}>{back}</Animated.View>
</Pressable>
);
}Code de cette leçon : App.tsx
import React from 'react';
import { View, Text, Pressable, StyleSheet } from 'react-native';
import Animated, {
useSharedValue,
useAnimatedStyle,
withSpring,
interpolate,
} from 'react-native-reanimated';
import * as Haptics from 'expo-haptics';
export default function App() {
const rotation = useSharedValue(0);
const isFlipped = useSharedValue(false);
const flip = () => {
// TODO : déclencher une vibration : Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
// TODO : faire pivoter la carte : rotation.value = withSpring(isFlipped.value ? 0 : 180);
// TODO : inverser le drapeau : isFlipped.value = !isFlipped.value;
};
const frontStyle = useAnimatedStyle(() => ({
// TODO : faire pivoter le recto de la carte autour de l'axe Y de rotation.value degrés :
// transform: [{ rotateY: `${rotation.value}deg` }],
// TODO : masquer le recto pendant le retournement avec interpolate :
// opacity: interpolate(rotation.value, [0, 90, 180], [1, 0, 0]),
}));
const backStyle = useAnimatedStyle(() => ({
// TODO : le verso est pivoté de 180 degrés supplémentaires :
// transform: [{ rotateY: `${rotation.value + 180}deg` }],
// TODO : afficher le verso seulement après le point médian :
// opacity: interpolate(rotation.value, [0, 90, 180], [0, 0, 1]),
}));
return (
<View style={styles.container}>
<Pressable onPress={flip}>
<Animated.View style={[styles.card, frontStyle]}>
<Text style={styles.cardText}>Recto</Text>
</Animated.View>
<Animated.View style={[styles.card, styles.cardBack, backStyle]}>
<Text style={styles.cardText}>Verso</Text>
</Animated.View>
</Pressable>
<Text style={styles.hint}>Touche pour retourner</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#1a5276',
},
card: {
width: 200,
height: 280,
borderRadius: 16,
backgroundColor: '#00d4ff',
justifyContent: 'center',
alignItems: 'center',
backfaceVisibility: 'hidden',
},
cardBack: {
position: 'absolute',
backgroundColor: '#16a085',
},
cardText: {
color: '#0a2540',
fontSize: 22,
fontWeight: '700',
},
hint: {
marginTop: 24,
color: '#aed6f1',
fontSize: 14,
},
});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 expo-haptics ?
2. Quels styles d'impact sont disponibles dans Haptics ?
Voici 2 des 4 questions de cette leçon. Réponds aux autres dans le jeu.
Tâches pratiques dans le jeu
- Éditeur de code
Complète le code de FlipCard.