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

后端 未结 4 1845
逝去的感伤
逝去的感伤 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:17

    Hi everyone, hope it's not too late...

    for anyone who is dealing with React Native animation on view's height.
    I know it is very annoying as:

    ✖️ React Native animation seems not supporting layout styles (e.g. width and height)
    ✖️ LayoutAnimation looks complicated to investigate
    ✖️ Wish to use an official way to animate instead of installing a third-party package
    ✖️ Sometimes content could be large to break your view styles

    so here is my solution for you (class component way):

    First, set the animated value in state:

    state = { height: new Animated.Value(0) };
    

    Next, set your animated view's max height with animation interpolation:

    const maxHeight = this.state.height.interpolate({ 
      inputRange: [0, 1], 
      outputRange: [0, 2000]  // <-- any value larger than your content's height
    };
    return (); 
    // any other fixed styles in styles.box
    

    After that, set the animation inside the function you called,
    or componentDidMount if you want it to show as soon as it rendered:

    // or in any function that users interact
    componentDidMount() {
      Animated.timing(this.state.formHeight, {
        toValue: 1,
        duration: 500,           // <-- animation duration
        easing: Easing.linear,   // <-- or any easing function
        useNativeDriver: false   // <-- need to set false to prevent yellow box warning
      }).start();
    }
    

    Be aware that don't set useNativeDriver to true as it is not supported on layout styles.


    Sample

    So below is a sample for you to interact with,
    feel free to copy and paste to your React Native project to have a try:

    import React, { PureComponent } from 'react';
    import { Animated, Button, Easing, View, Text, StyleSheet } from 'react-native';
    
    class AnimateBox extends PureComponent {
      state = { opacity: new Animated.Value(0), height: new Animated.Value(0) };
    
      showContent = () => {
        const { opacity, height } = this.state;
    
        Animated.timing(height, {
          toValue: 1,
          duration: 500,
          easing: Easing.linear,
          useNativeDriver: false  // <-- neccessary
        }).start(() => {
          Animated.timing(opacity, {
            toValue: 1,
            duration: 500,
            easing: Easing.linear,
            useNativeDriver: false  // <-- neccessary
          }).start();
        });
      };
    
      render() {
        const { opacity, height } = this.state;
        const maxHeight = height.interpolate({ 
          inputRange: [0, 1], 
          outputRange: [0, 1000]  // <-- value that larger than your content's height
        });
    
        return (
          
            
              
                Lorem Ipsum is simply a dummy text of the printing and typesetting industry.
                Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
                when an unknown printer took a galley of type and scrambled it to make a type specimen book.
                It has survived not only five centuries, but also the leap into electronic typesetting,
                remaining essentially unchanged. It was popularised in the 1960s with the release of
                Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
              
            
            
              

    Happy Coding :)

提交回复
热议问题