1

我正在尝试构建我的第一个动画,我只想让球体旋转(比如头部向左或向右转)。

我查看了动画 API,但出现以下错误:

使用“rotateY”键进行转换必须是字符串或数字:{“rotateY”:0}

就像我的动画对象无法被 react-vr 解析一样。

这是我尝试过的:

import React from 'react';

import {
  Animated,
  AppRegistry,
  asset,
  Pano,
  Sphere,
  View,
} from 'react-vr';

const sphereProps = {
  radius: 0.5,
  widthSegments: 20,
  heightSegments: 12
};

export default class Test extends React.Component {
  constructor() {
    super();

    this.state = {
      viewPoint: new Animated.Value(0)
    };
  }

  turnLeft = () => {
    Animated.timing(
      this.state.viewPoint,
      {
        toValue: -20,
        duration: 3000
      }
    ).start();
  }

  turnRight = () => {
    Animated.timing(
      this.state.viewPoint,
      {
        toValue: 20,
        duration: 3000
      }
    ).start();
  }

  render() {
    return (
      <Animated.View>
        <Pano source={asset('background.jpg')}/>

        <Sphere
          { ...sphereProps }
          style={{
            color: 'blue',
            transform: [
              { translate: [0, 0, -4] },
              { rotateY: this.state.viewPoint }
            ],
          }}
        />

      </Animated.View>
    );
  }

  componentDidMount() {
    this.turnLeft();
  }
};

AppRegistry.registerComponent('Test', () => Test);

即使我不调用该#turnLeft方法,也会在渲染组件时发生错误。

你知道我们应该如何实现这种动画旋转吗?

4

1 回答 1

0

您看到的错误是尝试将动画元素分配给尚未设置动画的元素。您可以为包含它的视图设置动画或为模型创建动画组件。

const AnimatedModel = Animated.createAnimatedComponent(Model);

于 2017-11-01T20:42:30.380 回答