探索React Native动画的无尽可能:React Native Animation
warning:
这篇文章距离上次修改已过186天,其中的内容可能已经有所变动。
import React, { useRef, useEffect } from 'react';
import { Animated, Easing, Text, View } from 'react-native';
const InfiniteAnimation = () => {
const fadeAnim = useRef(new Animated.Value(0)).current; // 初始透明度为0
useEffect(() => {
Animated.loop( // 使用Animated.loop创建无尽次数的动画
Animated.sequence([ // 使用Animated.sequence顺序执行动画
Animated.timing(fadeAnim, {
toValue: 1, // 透明度变为1(完全不透明)
duration: 1000, // 持续时间1000毫秒
easing: Easing.linear, // 线性变化的动画
useNativeDriver: true, // 使用原生动画驱动器
}),
Animated.timing(fadeAnim, {
toValue: 0, // 透明度变为0
duration: 1000, // 持续时间1000毫秒
easing: Easing.linear, // 线性变化的动画
useNativeDriver: true, // 使用原生动画驱动器
}),
]),
).start(); // 开始动画
}, [fadeAnim]);
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Animated.Text style={{ fontSize: 30, opacity: fadeAnim }}>
Fading Text
</Animated.Text>
</View>
);
};
export default InfiniteAnimation;
这段代码使用React Native的Animated API创建了一个无尽次数的淡入淡出动画。Animated.Value
用于跟踪透明度,通过Animated.timing
进行动画配置,并且使用Animated.loop
使动画无尽次数循环播放。代码中使用了函数组件和React的hook特性,即useRef
和useEffect
。
评论已关闭