如何在反应本机上运行一次动画
问题描述:
我希望当您按下按钮时,动画运行的setState
的btnLoaded
转到true
,这很好,但我希望一旦动画完成后,点击按钮,动画不再执行,因为它已经启动并完成。如何在反应本机上运行一次动画
我不明白该怎么做。
这里是动画的功能:
startButtonAnimation =() => {
this.animatedValue.setValue(0);
if (this.state.btnLoaded) {
Animated.timing(
this.animatedValue,
{
toValue: 1,
duration: 500,
useNativeDriver: true,
easing: Easing.bezier(0.15, 0.73, 0.37, 1.2)
}
).start();
}}
而且按钮的代码:
<TouchableOpacity
activeOpacity={0.9}
onPress={() => {
this.setState({
btnLoaded: true
});
setTimeout(() => this.startButtonAnimation(), 50);
}}>
代码在行动:
谢谢您的帮助!
答
动画是通过调用start()在你的动画开始。 start()需要一个完成回调,当动画完成时将会调用它。
此:
startButtonAnimation =() => {
this.animatedValue.setValue(0);
if (!this.state.btnLoaded) {
this.setState({
btnLoaded: true,
});
Animated.timing(
this.animatedValue,
{
toValue: 1,
duration: 500,
useNativeDriver: true,
easing: Easing.bezier(0.15, 0.73, 0.37, 1.2)
}
).start(() => {
}};
...
<TouchableOpacity
activeOpacity={0.9}
onPress={() => {
setTimeout(() => this.startButtonAnimation(), 50);
}}
>
可能做到这一点。你应该添加更多的代码来更好地理解发生了什么。
你好,它不能工作,因为在按钮onPress状态设置为true每次 – artSx
我要继续并告诉你,我还没有完全理解你的问题,因为代码是非常有限的:你可以添加一些关于你的“按钮”实际处理的代码,特别是你如何显示边界?它是通过评估btnLoaded? 对发生的事情有更多的了解会有所帮助。 – Nemoverflow
我有10/20/30按钮可以执行相同的动画功能。我希望只要其中一个按钮被按下,动画就无法播放。只有一次 – artSx