Animation 如何在运行动画序列时添加副作用?
我试图建立一个倒计时计时器,并为每一个计数我想播放一个声音。动画效果很好,但我想知道在序列中运行动画时是否可以播放声音 代码:Animation 如何在运行动画序列时添加副作用?,animation,react-native,Animation,React Native,我试图建立一个倒计时计时器,并为每一个计数我想播放一个声音。动画效果很好,但我想知道在序列中运行动画时是否可以播放声音 代码: Animated.sequence([ Animated.timing(this.state.moveY3, { toValue: 50, duration: 1000, useNativeDrive: true,
Animated.sequence([
Animated.timing(this.state.moveY3, {
toValue: 50,
duration: 1000,
useNativeDrive: true,
easing: Easing.spring
}), // play sound
Animated.timing(this.state.moveY3, {
toValue: 100,
duration: 100,
useNativeDrive: true,
}),
Animated.timing(this.state.moveY2, {
toValue: 50,
duration: 1000,
useNativeDrive: true,
easing: Easing.spring
}), //play sound
Animated.timing(this.state.moveY2, {
toValue: 100,
duration: 500,
useNativeDrive: true,
}),
Animated.timing(this.state.moveY1, {
toValue: 50,
duration: 1000,
useNativeDrive: true,
easing: Easing.spring
}), // play sound
Animated.timing(this.state.moveY1, {
toValue: 100,
duration: 500,
useNativeDrive: true,
}),
]).start()
注意:我知道如何播放声音,我使用react native sound软件包,我只是对如何在每次计数时播放声音感到困惑。进入每个动画的start()
方法。
因此,您可以将动画分解为更小的部分,而不是按顺序编写所有动画,如下所示:
// Run animation
animation1.start(() => {
playSound1();
Animated.sequence([
animation2,
animation3,
]).start(() => {
playSound2();
Animated.sequence([
animation4,
animation5,
]).start(() => {
playSound3();
animation6.start();
})
})
});
// Move animations into variables so that the execution of the animation is more readable
const animation1 = Animated.timing(this.state.moveY3, {
toValue: 50,
duration: 1000,
useNativeDrive: true,
easing: Easing.spring
});
const animation2 = Animated.timing(this.state.moveY3, {
toValue: 100,
duration: 100,
useNativeDrive: true,
}),
const animation3 = Animated.timing(this.state.moveY2, {
toValue: 50,
duration: 1000,
useNativeDrive: true,
easing: Easing.spring
}),
...
通过将第一位中的一些内容移动到其他函数中,您可以稍微减少嵌套并使其更具可读性。。但它应该是这样工作的。回答得很好