jsReact Native:如何使用translateX为视图的位置设置动画?

时间:2016-12-14 22:26:22

标签: javascript react-native

当我按下“Enter Now”按钮时,我试图将我的视图移出屏幕:

Screenshot of app with arrows

export class Onboarding extends Component {

  constructor(props) {
    super(props);
    this.state = {
      offsetX: new Animated.Value(0),
    }
  }

  hideOnboarding() {
    console.log("hiding"); // <-------- prints out when button pressed
    Animated.timing(
      this.state.offsetX,
      { toValue: new Animated.Value(-100) }
    ).start();
  }

  render() {
    return (
      <Animated.View style={{ transform: [{translateX: this.state.offsetX}] }}>
        ...
        <TouchableOpacity onPress={ () => { this.hideOnboarding() } }>
          <View style={styles.enterButton}>
            <Text style={styles.buttonText}>Enter Now</Text>
          </View>
        </TouchableOpacity>
      </Animated.View>
    );

但是点按我的按钮时没有任何动静。我的控制台声明有效。有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:4)

我认为你需要改变:

if(e.getCode() == KeyCode.CIRCUMFLEX)

{ toValue: new Animated.Value(-100) }