react-native - 如何在 React Native 中立即停止循环动画
问题描述
我正在尝试在本机反应中实现循环动画。我也可以停止动画,但只有在完成正在运行的动画后才会停止。有没有办法立即阻止它。由于我正在改变state
value ,我认为我不能使用Animated.loop
.
编辑:我尝试使用停止动画
Animated.timing(this.state.opacity).stop()
但这对动画没有影响,它只是继续。所以我添加了一个布尔值来控制它,它有效,但只有在完成正在进行的动画循环之后。
这是我的代码
this.state = {
varyingText: 'location',
stopAnimation: false,
opacity: new Animated.Value(0),
};
componentDidMount = () => {
this.startAnimation();
};
startAnimation = () => {
if (this.state.stopAnimation) return;
Animated.timing(this.state.opacity, {
toValue: 1,
duration: 1000
}).start(() => {
Animated.timing(this.state.opacity, {
toValue: 0,
duration: 1000
}).start(() => {
this.setState({
varyingText: 'location 1',
}, () => {
Animated.timing(this.state.opacity, {
toValue: 1,
duration: 1000
}).start(() => {
Animated.timing(this.state.opacity, {
toValue: 0,
duration: 1000
}).start(() => {
this.setState({
varyingText: 'location 2',
}, () => {
Animated.timing(this.state.opacity, {
toValue: 1,
duration: 1000
}).start(() => {
Animated.timing(this.state.opacity, {
toValue: 0,
duration: 1000
}).start(() => {
this.setState({
varyingText: 'location 3',
}, () => this.startAnimation())
})
})
})
})
})
})
})
});
};
stopAnimation = () => {
this.setState({
stopAnimation: true,
});
};
render() {
const opacityStyle = {
opacity: this.state.opacity,
};
return (
<View style={styles.view}>
<Animated.Text style={...opacityStyle}>
{this.state.varyingText}
</Animated.Text>
<Button
onPress={this.stopAnimation}
title="Stop"
color="#841584"
/>
</View>
);
}
在代码中,如果我在 text 为 时停止动画location 1
,它会在显示后停止location 2
。
解决方案
推荐阅读
- php - CodeIgniter 路由问题以访问前端和后端文件夹
- authentication - Redux Navigation with Redux ,一旦通过身份验证导航到主屏幕
- ruby-on-rails - 如何从选择标签栏搜索数据
- windows - aws s3 sync --recursive 在 Windows 中不起作用
- keycloak - Keycloak: Admin-cli Add SMTP server details?
- java - 在java中从对象转换为字符串
- python - Pythonic方式以并行方式迭代字典内的列表作为字典
- google-apps-script - 通过 getLastRow 控制电子表格最后一行的确定
- java - JAR 不存在或不是普通文件
- python - 用于查找逗号在字母之前的每次正则表达式