How to slide in and out from the bottom in React Native?

前端 未结 3 2045
天命终不由人
天命终不由人 2020-12-14 08:30

In React Native iOS, I would like to slide in and out of a like in the following picture.

In the following example, when a button is pressed, the Payment Inf

相关标签:
3条回答
  • 2020-12-14 08:36

    I know it is a little bit late, but thought it might be useful for someone. You should try out a component called rn-sliding-out-panel. It works awesomely. https://github.com/octopitus/rn-sliding-up-panel

    <SlidingUpPanel
        draggableRange={top: 1000, bottom: 0}
        showBackdrop={true|false /*For making it modal-like*/}
        ref={c => this._panel = c}
        visible={ture|false /*If you want it to be visible on load*/}
    ></SlidingUpPanel>
    

    And you can even open it from an external button:

    <Button onPress={()=>{this._panel.transitionTo(1000)}} title='Expand'></Button>
    

    You can install it via npm: sudo npm install rn-sliding-out-panel --save on your react-native root directory.


    I hope it helps someone :D

    0 讨论(0)
  • 2020-12-14 08:37

    After a quite long search I found very good library called react-native-swipe-down with MIT licence. It will help you make a slider <View /> with no effort.

    I Hope this library help you out.

    import SwipeUpDown from 'react-native-swipe-up-down';
    
    <SwipeUpDown        
        itemMini={<ItemMini />} // Pass props component when collapsed
        itemFull={<ItemFull />} // Pass props component when show full
        onShowMini={() => console.log('mini')}
        onShowFull={() => console.log('full')}
        onMoveDown={() => console.log('down')}
        onMoveUp={() => console.log('up')}
        disablePressToShow={false} // Press item mini to show full
        style={{ backgroundColor: 'green' }} // style for swipe
    />
    
    0 讨论(0)
  • 2020-12-14 08:40

    Basically, you need to absolute-position your view to the bottom of the screen. Then you translate its y value to equal its height. (The sub view must have a specific height in order to know how much to move it)

    Here's a playground showing the result: https://rnplay.org/apps/n9Gxfg

    Code:

    'use strict';
    
    import React, {Component} from 'react';
    import ReactNative from 'react-native';
    
    const {
      AppRegistry,
      StyleSheet,
      Text,
      View,
      TouchableHighlight,
      Animated
    } = ReactNative;
    
    
    var isHidden = true;
    
    class AppContainer extends Component {
      constructor(props) {
        super(props);
        this.state = {
          bounceValue: new Animated.Value(100),  //This is the initial position of the subview
          buttonText: "Show Subview"
        };
      }
    
    
      _toggleSubview() {    
        this.setState({
          buttonText: !isHidden ? "Show Subview" : "Hide Subview"
        });
    
        var toValue = 100;
    
        if(isHidden) {
          toValue = 0;
        }
    
        //This will animate the transalteY of the subview between 0 & 100 depending on its current state
        //100 comes from the style below, which is the height of the subview.
        Animated.spring(
          this.state.bounceValue,
          {
            toValue: toValue,
            velocity: 3,
            tension: 2,
            friction: 8,
          }
        ).start();
    
        isHidden = !isHidden;
      }
    
      render() {
        return (
          <View style={styles.container}>
              <TouchableHighlight style={styles.button} onPress={()=> {this._toggleSubview()}}>
                <Text style={styles.buttonText}>{this.state.buttonText}</Text>
              </TouchableHighlight>
              <Animated.View
                style={[styles.subView,
                  {transform: [{translateY: this.state.bounceValue}]}]}
              >
                <Text>This is a sub view</Text>
              </Animated.View>
          </View>
        );
      }
    }
    
    var styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
        marginTop: 66
      },
      button: {
        padding: 8,
      },
      buttonText: {
        fontSize: 17,
        color: "#007AFF"
      },
      subView: {
        position: "absolute",
        bottom: 0,
        left: 0,
        right: 0,
        backgroundColor: "#FFFFFF",
        height: 100,
      }
    });
    
    AppRegistry.registerComponent('AppContainer', () => AppContainer);
    
    0 讨论(0)
提交回复
热议问题