与其使用setInterval,不如启动
Animated.timing
动画与故事的持续时间相同?如果用户暂停报道,您应该可以调用
stop()
在动画上停止它,然后用预期的剩余持续时间再次继续。
另外,与其设置进度条宽度的动画,不如使用
scaleX
或
translateX
在进度条上,可以完全在本机线程上运行。
下面是一个部分示例,缺少触发
onStart
和
onPause
因为我不知道你的其他组件是什么样子的。
class Thing {
_progressValue = new Animated.Value(0);
onStart = (duration: number) => {
this._animation = Animated.timing(this._progressValue, {
duration: duration,
toValue: 100,
easing: Easing.linear,
useNativeDriver: true,
}).start();
};
onPause = () => {
if (this._animation != null) this._animation.stop();
};
render() {
return (
<View style={{width: 100, height: 5, backgroundColor: 'green'}}>
<Animated.View style={{width: 100, height: 5, backgroundColor: 'red', transform: [{translateX: this._progressValue}]}} />
</View>
);
}
}