ReactJS:用于sin和cos的AnimationValue.interpolate

Mou*_*ous 3 reactjs react-native react-animated

我正在尝试对ReactJS本机中的组件进行动画处理,以使其以圆形的方式围绕点连续旋转。基本上,我需要一个不断增加的计时器,可以用来将X,Y坐标设置为类似 {x: r * Math.sin(timer), y: r * Math.cos(timer)}

我发现该指南包含有关插值的一些非常有用的信息,但是我仍然没有成功:

http://browniefed.com/react-native-animation-book/INTERPOLATION.html

我如何像这样更新组件的位置?

额外的问题:如何使动画连续?

Val*_*Val 5

由于本机插值不支持函数回调,因此我创建了一个辅助函数:

function withFunction(callback) {
    let inputRange = [], outputRange = [], steps = 50;
    /// input range 0-1
    for (let i=0; i<=steps; ++i) {
        let key = i/steps;
        inputRange.push(key);
        outputRange.push(callback(key));
    }
    return { inputRange, outputRange };
}
Run Code Online (Sandbox Code Playgroud)

将动画应用到translationX和translateY:

transform: [
{
   translateX:
      this.rotate.interpolate(
         withFunction( (value) => Math.cos(Math.PI*value*2) * radius )
      )
},
{
   translateY:
      this.rotate.interpolate(
         withFunction( (value) => Math.sin(Math.PI*value*2) * radius )
      )
},
]
Run Code Online (Sandbox Code Playgroud)

视觉结果:

在此处输入图片说明

为了使动画连续,我做了如下操作:

rotate = new Animated.Value(0);

componentDidMount() {
    setInterval( () => {
        let step = 0.05;
        let final = this.rotate._value+step;
        if (final >= 1) final = 0;
        this.rotate.setValue(final);
    }, 50);
}
Run Code Online (Sandbox Code Playgroud)