Kurs React Native · Moduł 10: Animacje
Animated API - Podstawy animacji
W tej lekcji3
Witaj w Galerii Prądów! Naucz się animować elementy jak prądy morskie!
Podstawowa animacja
import { Animated, Pressable, StyleSheet } from 'react-native';
import { useRef } from 'react';
function FadeInView() {
const fadeAnim = useRef(new Animated.Value(0)).current;
const fadeIn = () => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: 500,
useNativeDriver: true,
}).start();
};
const fadeOut = () => {
Animated.timing(fadeAnim, {
toValue: 0,
duration: 500,
useNativeDriver: true,
}).start();
};
return (
<View>
<Animated.View style={[styles.box, { opacity: fadeAnim }]}>
<Text>Znikający element</Text>
</Animated.View>
<Pressable onPress={fadeIn}><Text>Pokaż</Text></Pressable>
<Pressable onPress={fadeOut}><Text>Ukryj</Text></Pressable>
</View>
);
}Różne typy animacji
// timing - liniowa animacja
Animated.timing(value, { toValue: 1, duration: 300, useNativeDriver: true });
// spring - efekt sprężyny
Animated.spring(value, { toValue: 1, friction: 5, tension: 40, useNativeDriver: true });
// decay - spowalnianie
Animated.decay(value, { velocity: 0.5, deceleration: 0.997, useNativeDriver: true });
// Łączenie animacji
Animated.sequence([anim1, anim2]); // po kolei
Animated.parallel([anim1, anim2]); // równolegle
Animated.stagger(100, [anim1, anim2, anim3]); // z opóźnieniemInterpolacja
const animValue = useRef(new Animated.Value(0)).current;
const rotation = animValue.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
});
const backgroundColor = animValue.interpolate({
inputRange: [0, 0.5, 1],
outputRange: ['#1a5276', '#00d4ff', '#1a5276'],
});
<Animated.View style={{ transform: [{ rotate: rotation }], backgroundColor }} />Kod do tej lekcji: App.tsx
import React, { useRef } from 'react';
import { Animated, View, Text, Pressable, StyleSheet } from 'react-native';
export default function App() {
// TODO: utwórz animowaną wartość startującą od 0
// użyj useRef, aby przetrwała re-rendery:
// const fadeAnim = useRef(new Animated.Value(0)).current;
const fadeIn = () => {
// TODO: uruchom Animated.timing na fadeAnim:
// toValue: 1, duration: 500, useNativeDriver: true
// pamiętaj o wywołaniu .start() na końcu
};
const fadeOut = () => {
// TODO: uruchom Animated.timing na fadeAnim:
// toValue: 0, duration: 500, useNativeDriver: true, a potem .start()
};
return (
<View style={styles.container}>
{/* TODO: podłącz fadeAnim do stylu opacity animowanego widoku:
style={[styles.box, { opacity: fadeAnim }]} */}
<Animated.View style={styles.box}>
<Text style={styles.boxText}>Prąd morski</Text>
</Animated.View>
<Pressable style={styles.button} onPress={fadeIn}>
<Text style={styles.buttonText}>Pokaż</Text>
</Pressable>
<Pressable style={styles.button} onPress={fadeOut}>
<Text style={styles.buttonText}>Ukryj</Text>
</Pressable>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#1a5276',
},
box: {
width: 200,
height: 200,
borderRadius: 16,
backgroundColor: '#00d4ff',
justifyContent: 'center',
alignItems: 'center',
marginBottom: 24,
},
boxText: {
color: '#0a2540',
fontSize: 18,
fontWeight: '700',
},
button: {
paddingVertical: 12,
paddingHorizontal: 28,
borderRadius: 8,
backgroundColor: '#2980b9',
marginVertical: 6,
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
});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 utworzyć animowaną wartość?
2. Dlaczego używamy useRef z Animated.Value?
To 2 z 7 pytań do tej lekcji. Pozostałe rozwiążesz w grze.
Zadania praktyczne w grze
- Klikanie w kolejności
Kliknij elementy Animated.timing:
- Układanie w poziomie
Ułóż uruchomienie animacji z callbackiem:
- Układanie w pionie
Uporządkuj interpolację:
- Edytor kodu
Uzupełnij kod FadeInView.