Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsReact Native: How to animate a view's position with translateX?

I am trying to move my view off screen when the "Enter Now" button is pressed:

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>
    );

But nothing is moving when I tap my button. My console statement works though. Does anyone know what I'm doing wrong?

like image 651
bigpotato Avatar asked Dec 14 '16 22:12

bigpotato


People also ask

How to move views in native translatey in React Native?

React Native TranslateY animation property style is used to move views in vertical Axis. Here if you enter animation toValue in + Plus then it will move views top to bottom. If we would pass toValue in – Minus then it will move view from bottom to top again.

How to move object view from top to bottom using translatey position animation?

Here if you enter animation toValue in + Plus then it will move views top to bottom. If we would pass toValue in – Minus then it will move view from bottom to top again. So in this tutorial we would move object view from top to bottom using TranslateY Position Animation Android iOS Tutorial.

How to get started with React Native?

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

How do I use the native driver for animation?

You can use the native driver by specifying useNativeDriver: true in your animation configuration. See the Animations guide to learn more. Only animatable components can be animated.


1 Answers

I think you need to change:

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

to

{ toValue: -100 }
like image 98
Matt Aft Avatar answered Sep 28 '22 08:09

Matt Aft