不变违规:AnimatedValueXY必须使用数字对象或AnimatedValues初始化

时间:2018-03-02 02:34:54

标签: react-native expo react-animated

import React, { Component } from 'react';
import { View, StyleSheet, Animated } from 'react-native';

export default class Ball extends Component {
  componentWillMount(){
    this.position = new Animated.ValueXY(0,0);
    Animated.spring(this.position, {
      toValue: {x :200, y: 500}
    }).start();
  }

  render() {
    return (
     <Animated.View style={this.position.getLayout()}>
        <View style={styles.ball} />
      </Animated.View>
    );
  }
}

const styles = StyleSheet.create({
  ball: {
    height: 60,
    width: 60,
    borderRadius: 30,
    borderWidth: 30,
    borderColor: 'green'
      }
    });

enter image description here

如果将Animated.ValueXY(100,100)更改为Animated.ValueXY(0,0)

,则上面图片中附带的代码运行没有问题

任何人都可以解释为什么这样的行为存在,因为我的想法不是让这个球从x:0,y:0开始移动我希望它开始从x:100,y:100移动

1 个答案:

答案 0 :(得分:1)

this.position = new Animated.ValueXY({ x: 100, y: 100 });

试试这个