Oto przykładowe rozwiązanie dla React Native z użyciem react-native-reanimated do animacji zmiany tekstu:
jsx Copy code
import React, { useState, useEffect } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Animated, {
useSharedValue,
useAnimatedStyle,
withTiming,
withRepeat,
withSequence,
Easing
} from 'react-native-reanimated';
const TextAnimationExample = () => {
const [currentTextIndex, setCurrentTextIndex] = useState(0);
const texts = [
'Pierwszy tekst',
'Drugi tekst',
'Trzeci tekst'
];
// Animacja opacity dla efektu migania
const opacity = useSharedValue(1);
// Styl animowany
const animatedStyle = useAnimatedStyle(() => {
return {
opacity: opacity.value
};
});
useEffect(() => {
// Ustawienie interwału zmiany tekstu co 1 sekundę
const interval = setInterval(() => {
if (currentTextIndex < texts.length - 1) {
setCurrentTextIndex(prev => prev + 1);
// Animacja migania
opacity.value = withRepeat(
withSequence(
withTiming(0, { duration: 500, easing: Easing.linear }),
withTiming(1, { duration: 500, easing: Easing.linear })
),
2, // 2 powtórzenia (razem 4 stany opacity)
false // nie odwraca animacji
);
}
}, 1000);
// Czyszczenie interwału
return () => clearInterval(interval);
}, [currentTextIndex]);
return (
<View style={styles.container}>
<Animated.Text
style={[
styles.text,
animatedStyle,
// Ostatni tekst zostaje na stałe
currentTextIndex === texts.length - 1 && { opacity: 1 }
]}
>
{texts[currentTextIndex]}
</Animated.Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 24,
fontWeight: 'bold',
},
});
export default TextAnimationExample;