React Native UI界面还原,组件布局与动画效果
import React, { useState } from 'react';
import { Animated, Text, View, StyleSheet } from 'react-native';
const FadingText = ({ text }) => {
const [fadeAnim] = useState(new Animated.Value(1));
React.useEffect(() => {
Animated.sequence([
Animated.timing(fadeAnim, {
toValue: 0,
duration: 1000,
useNativeDriver: true
}),
Animated.timing(fadeAnim, {
toValue: 1,
duration: 1000,
delay: 1000,
useNativeDriver: true
})
]).start(() => {
// 循环动画
setInterval(() => {
Animated.sequence([
Animated.timing(fadeAnim, {
toValue: 0,
duration: 1000,
useNativeDriver: true
}),
Animated.timing(fadeAnim, {
toValue: 1,
duration: 1000,
delay: 1000,
useNativeDriver: true
})
]).start();
}, 2000);
});
}, []);
return (
<Animated.Text style={{ ...styles.text, opacity: fadeAnim }}>
{text}
</Animated.Text>
);
};
const styles = StyleSheet.create({
text: {
fontSize: 20,
color: 'blue'
}
});
export default FadingText;
这段代码展示了如何在React Native应用中创建一个文本组件,该组件可以在一定的时间间隔内循环淡入淡出的动画效果。使用了React Hooks(useState和useEffect),以及Animated API来实现动画。
评论已关闭