Kurs React Native · Moduł 10: Animacje
expo-haptics - Wibracje
Dodaj feedback haptyczny do interakcji!
npx expo install expo-hapticsimport * as Haptics from 'expo-haptics';
// Różne typy wibracji
const lightVibration = () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
const mediumVibration = () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
const heavyVibration = () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
// Powiadomienia
const success = () => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
const warning = () => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
const error = () => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
// Selekcja
const selection = () => Haptics.selectionAsync();
// Użycie z gestem
const panGesture = Gesture.Pan()
.onStart(() => {
runOnJS(Haptics.selectionAsync)();
});Projekt: Animowana karta
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>
);
}Kod do tej lekcji: 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: wywołaj wibrację: Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
// TODO: obróć kartę: rotation.value = withSpring(isFlipped.value ? 0 : 180);
// TODO: zmień flagę: isFlipped.value = !isFlipped.value;
};
const frontStyle = useAnimatedStyle(() => ({
// TODO: obróć przód karty wokół osi Y o rotation.value stopni:
// transform: [{ rotateY: `${rotation.value}deg` }],
// TODO: ukryj przód przy obrocie używając interpolate:
// opacity: interpolate(rotation.value, [0, 90, 180], [1, 0, 0]),
}));
const backStyle = useAnimatedStyle(() => ({
// TODO: tył jest obrócony o dodatkowe 180 stopni:
// transform: [{ rotateY: `${rotation.value + 180}deg` }],
// TODO: pokaż tył dopiero po połowie obrotu:
// 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}>Przód</Text>
</Animated.View>
<Animated.View style={[styles.card, styles.cardBack, backStyle]}>
<Text style={styles.cardText}>Tył</Text>
</Animated.View>
</Pressable>
<Text style={styles.hint}>Dotknij, aby obrócić</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,
},
});Hasła z dokumentacji w tej lekcji
Widzisz błąd w tej lekcji?
Sprawdź się
Odpowiedz na pytania z tej lekcji. Wybierz odpowiedź, a od razu zobaczysz, czy jest poprawna.
1. Jak zainstalować expo-haptics?
2. Jakie style impact są dostępne w Haptics?
To 2 z 4 pytań do tej lekcji. Pozostałe rozwiążesz w grze.
Zadania praktyczne w grze
- Edytor kodu
Uzupełnij kod FlipCard.