1
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'
  }
});

在此处输入图像描述

Animated.ValueXY(100,100)上图所附的代码改成后运行没有问题Animated.ValueXY(0,0)

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

4

1 回答 1

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

尝试这个

于 2018-03-16T10:43:25.973 回答