【发布时间】:2021-02-14 14:34:48
【问题描述】:
我在重新创建一个 CSS loader animation 在带有动画 API 的 React Native 中。
我的 React Native 尝试: screen record.
CSS代码如下:
.whitePillar {
width: 8px;
height: 50px;
margin-right:5px;
background-color: #FFF;
animation: animation 1s infinite;
float: left;
}
.whitePillar :nth-child(10) {
animation-delay: 0.9s;
}
.whitePillar :nth-child(9){
-webkit-animation-delay: 0.8s;
animation-delay: 0.8s;
}
.whitePillar :nth-child(8) {
animation-delay: 0.7s;
}
.whitePillar :nth-child(7){
-webkit-animation-delay: 0.6s;
animation-delay: 0.6s;
}
.whitePillar :nth-child(6) {
animation-delay: 0.5s;
}
.whitePillar :nth-child(5) {
animation-delay: 0.4s;
}
.whitePillar :nth-child(4){
-webkit-animation-delay: 0.3s;
animation-delay: 0.3s;
}
.whitePillar :nth-child(3) {
animation-delay: 0.2s;
}
.whitePillar :nth-child(2) {
animation-delay: 0.1s;
}
@keyframes animation {
50% {
transform: translateX(-25px) scaleY(0.5);
}
}
React Native 代码如下:
<View style={styles.loaderContainer}>
<View style={styles.pillarsWrapper}>
{[...Array(numberOfPillars)].map((value, index) => (
<LoaderPillar
delay={index*100}
key={index}
/>
))}
</View>
</View>
LoaderPillar 代表每一个单独的白色柱子:
<Animated.View
style={[styles.pillar, pillarAnimation]}
/>
我已经使用这个在 LoaderPillar 上完成了动画:
const [ pillarAnimationTranslate ] = useState(new Animated.Value(0));
const [ pillarAnimationScale ] = useState(new Animated.Value(1));
const pillarAnimation = {
transform: [{translateX: pillarAnimationTranslate}, {scaleY: pillarAnimationScale}]
};
useEffect(() => {
Animated.loop(Animated.timing(pillarAnimationScale, {
toValue: 0.5,
duration: 500,
delay: delay,
useNativeDriver: true,
easing: Easing.ease
})).start();
}, [pillarAnimationScale]);
useEffect(() => {
Animated.loop(Animated.timing(pillarAnimationTranslate, {
toValue: -25,
duration: 500,
delay: delay,
useNativeDriver: true,
easing: Easing.ease
})).start()
}, [pillarAnimationTranslate]);
我是 React Native 动画的新手,所以我什至不知道我是否走在正确的轨道上。我认为问题在于 CSS 中的关键帧部分——那个百分比(50%)——我不知道如何重新创建它。 任何有关升级甚至完全改变的建议将不胜感激。提前谢谢你。
【问题讨论】:
标签: react-native animation css-animations