我在svg路径下面,该路径在每个value
状态更改中都成功绕原点旋转,其中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}
/>
这完全无法与使用state的先前工作代码相提并论。原因之一是动画API不支持originX,originY值,我也不知道如何替换,但我不确定这是唯一的原因,也许rotate
与{{1 }}属性吗?
因此,问题是,如何使用动画api通过连续调用setState的代码实现相同的结果?