📜  react native anination 2 valuse - Javascript(1)

📅  最后修改于: 2023-12-03 15:19:43.438000             🧑  作者: Mango

React Native Animation 2 Values

React Native provides a powerful animation API that allows you to create dynamic and engaging user interfaces. With the Animation API, you can animate various properties of your UI components, such as position, opacity, scale, and rotation.

In this tutorial, we'll explore how to use the Animation API to animate between two values. We'll start by covering the basics of the Animated library, then we'll dive into coding examples that demonstrate how to use it.

Animated Library

The Animated library is a core part of the React Native animation infrastructure. It provides a set of classes and methods that enable you to create animations.

To start using the Animated library, you need to import it from the react-native package:

import { Animated } from 'react-native';

The most important class in the Animated library is the Animated.Value class. This class represents a value that can be animated. You can create an instance of this class by calling the new Animated.Value(initialValue) function. The initialValue parameter is the starting value for the animated value.

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

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      animatedValue: new Animated.Value(0),
    };
  }

  componentDidMount() {
    Animated.timing(this.state.animatedValue, {
      toValue: 1,
      duration: 1000,
      useNativeDriver: true,
    }).start();
  }

  render() {
    const { animatedValue } = this.state;
    const opacity = animatedValue.interpolate({
      inputRange: [0, 1],
      outputRange: [0, 1],
    });
    return (
      <Animated.View style={{ opacity }}>
        <Text>Hello, World!</Text>
      </Animated.View>
    );
  }
}

export default App;

In this code snippet, we create a new instance of the Animated.Value class in the constructor() method of our component. We then use this value to animate the opacity of a Text component using the interpolate() method.

The interpolate() method maps the input range of the animated value to the output range of some other value, in this case, the opacity of our View component.

Animating Between Two Values

To animate between two values, we can use the Animated.timing() method. This method creates a new timing animation that transitions the animated value from one value to another over a set amount of time.

Here's an example that demonstrates how to use the Animated.timing() method to animate between two values:

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

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      animatedValue: new Animated.Value(0),
    };
  }

  componentDidMount() {
    Animated.timing(this.state.animatedValue, {
      toValue: 1,
      duration: 1000,
      useNativeDriver: true,
    }).start(() => {
      Animated.timing(this.state.animatedValue, {
        toValue: 0,
        duration: 1000,
        useNativeDriver: true,
      }).start();
    });
  }

  render() {
    const { animatedValue } = this.state;
    const opacity = animatedValue.interpolate({
      inputRange: [0, 1],
      outputRange: [1, 0],
    });
    const translateY = animatedValue.interpolate({
      inputRange: [0, 1],
      outputRange: [0, 50],
    });
    return (
      <Animated.View style={{ opacity, transform: [{ translateY }] }}>
        <Text>Hello, World!</Text>
      </Animated.View>
    );
  }
}

export default App;

In this code snippet, we create a new instance of the Animated.Value class in the constructor() method of our component. We then use this value to animate the opacity and translateY of a View component using the interpolate() method.

Inside the componentDidMount() method, we create two separate timing animations that transition the animated value from 0 to 1 and back to 0 over a set amount of time. We use the start() method to begin each animation.

Conclusion

Animating between two values is easy with the React Native Animation API. Using the Animated.timing() method, you can create animations that transition between two values over a set amount of time.

In this tutorial, we've covered the basics of the Animated library and demonstrated how to use it to create animations. With the knowledge you've gained here, you should be able to create even more advanced animations for your React Native applications.