How do you animate the height in react native when you don't know the size of the content?

后端 未结 4 1848
逝去的感伤
逝去的感伤 2021-01-03 17:16

In react-native, how do you animate the size of a View when you dont know the size of it\'s contents?

Let\'s say the View\'s content height can be

4条回答
  •  悲哀的现实
    2021-01-03 18:07

    You are going to have to add some kind of size scaling, probably with percentages for best effect.

    First thing first you would need to use Animated.View instead of View.

    Next you would need to apply a transform to the style of the view lets say it looks like below. This is the part that updates and changes and creates the motion.

            
    

    This next part is basically an animated API example, you would write something like this (custom to how you want) and when this script is called it will animate in whatever way you specify.

      Animated.timing(                    // Animate over time
        this.state.ViewScale,             // The animated value to drive, this would be a new Animated.Value(0) object.
        {
          toValue: 100,                   // Animate the value
          duration: 5000,                 // Make it take a while
        }
      ).start();
    

    Lastly you will probably want to apply an interpolation to the value to make it look as custom as possible.

    (this will go into your render() function but before the return(). the ViewScaleValue will go into the Animated.View transform)

    const ViewScaleValue = this.state.ViewScale.interpolate({
      inputRange: [0, 25, 50, 75, 100],
      outputRange: [0, .5, 0.75, 0.9, 1]
    });
    

    all of this code would make ViewScaleValue, a simple number, animate from 0-100, going fast and then slow (because of interpolation) and apply each iteration of the animation to the Animated.View transform.

    Read the Animated API alongside this to get a good grasp on it.

提交回复
热议问题