【发布时间】:2020-01-11 12:50:32
【问题描述】:
我有以下 svg 路径,它在每个value 状态更改成功时围绕原点旋转,其中值是一些度数值:
<Path
transform={{
rotation: this.state.value,
originX: width / 2,
originY: height / 2
}}
d={`M ${width * 0.5} ${height * 0.5} L${width * 0.75} ${height * 0.75}`} //the actual path does not matter
fill={Colors.black}
stroke={Colors.black}
/>
我每隔约 20 毫秒在 value 上调用 setState,但这不够平滑,+ 这不是进行转换的推荐方式。
我想要实现的是使用动画 API 为旋转设置动画,以实现更平滑的旋转。
我尝试将我的value 状态设为Animated.Value。然后将Path 设为Animated.createAnimatedComponent(Path) 并调用:
Animated.timing(this.state.value, {
toValue: newDegreesValue,
duration: 1000,
useNativeDriver: true
}).start();
然后我像这样渲染路径:
<Path
style={transform:[{
rotate: this.state.value
}]}
d={`M ${width * 0.5} ${height * 0.5} L${width * 0.75} ${height * 0.75}`} //the actual path does not matter
fill={Colors.black}
stroke={Colors.black}
/>
这在接近之前使用状态的工作代码时根本不起作用。一个原因是动画 API 不支持的 originX、originY 值,我不知道如何替换,但我不确定这是唯一的原因,也许 rotate 与 rotation 属性也有所不同?.
那么问题来了,不断调用 setState 的代码如何使用动画 api 实现同样的结果呢?
【问题讨论】:
标签: react-native animation react-native-svg