ReactNative 侧滑菜单的实现

  侧滑菜单在App的框架结构非常常见和实用,本篇博客,是用reactNative实现了一个侧滑的基本的框架,但具体的菜单栏需要各位看官的去自行添加了

实现的思路 tab+定时器+Animated.View的方式



import React, { Component, PropTypes } from 'react'
import {
  View, Text, StyleSheet,
  TouchableHighlight, Animated,
  Platform, Dimensions, Image
} from "react-native"


const WINDOW = Dimensions.get('window')
var closeTimeoutId = null

export default class MyInfo extends Component {
  static propTypes = {
    onClose: PropTypes.func,
    onCancel: PropTypes.func,
    showCancel: PropTypes.bool
  }
  static defaultProps =  {
    onClose: null,
    onCancel: null,
    closeInterval: 4000,
    showCancel: false,

  }
  constructor(props) {
    super(props)
    this.state = {
      animationValue: new Animated.Value(0),
      duration: 450,
      isOpen: false,
      topValue: 0
    }
    // Render
    this.renderButton = this.renderButton.bind(this)
    this.renderDropDown = this.renderMyinfo.bind(this)
    // Action
    this.alert = this.alert.bind(this)
    this.dismiss = this.dismiss.bind(this)
    this.onCancel = this.onCancel.bind(this)
    this.onClose = this.onClose.bind(this)
    // Util
    this.animate = this.animate.bind(this)

  }

  alert() {

    if (this.state.isOpen) {
      this.dismiss()
      return
    }
    if (this.state.isOpen == false) {
      this.setState({
        isOpen: true,
        topValue: 0
      })
    }
    this.animate(1)
     if (this.props.closeInterval > 1) {
      closeTimeoutId = setTimeout(function() {
        this.onClose()
      }.bind(this), this.props.closeInterval)
    }
  }
  dismiss(onDismiss) {
    if (this.state.isOpen) {
      if (closeTimeoutId != null) {
        clearTimeout(closeTimeoutId)
      }
      this.animate(0)
      setTimeout(function() {
        if (this.state.isOpen) {
          this.setState({
            isOpen: false
          })
          if (onDismiss) {
   
            onDismiss('warn')
          }
        }
      }.bind(this), (this.state.duration))
    }
  }
  onClose() {
    this.dismiss(this.props.onClose)
  }
  onCancel() {
    this.dismiss(this.props.onCancel)
  }
  animate(toValue) {
    Animated.spring (
      this.state.animationValue, {
        toValue: toValue,
        duration: this.state.duration,
        friction: 9
      }
    ).start()
  }


  
  renderButton( onPress, isRendered) {
    if ( isRendered) {
      return (
         <View style={{top:0,width:WINDOW.width,height:WINDOW.height,backgroundColor:'green'}}>

             <TouchableHighlight  onPress={onPress} >
               <Image style={styles.cancelBtnImageStyle} source={require('./assets/cancel.png')} />

              </TouchableHighlight>
         </View>

    
      )
    }
    return null
  }
  renderMyinfo(isOpen) {
    if (isOpen == true) {

          style = {backgroundColor: '#cd853f'}

      return (
          <Animated.View
           ref={(ref) => this.mainView = ref}
           style={{
              transform: [{
                translateX: this.state.animationValue.interpolate({
                  inputRange: [0, 1],
                  outputRange: [-WINDOW.width, 0]
                }),
              }],
              position: 'absolute',
              top: this.state.topValue,
              left: 0,
              right: 0
            }}>
              <View style={ {backgroundColor: '#cd853f'}}>
                {this.renderButton(  this.onCancel, this.props.showCancel)}
              </View>
          </Animated.View>
      )
    }
    return null
  }
  render() {
    return (
      this.renderMyinfo(this.state.isOpen)
    )
  }
}

let styles= StyleSheet.create({
    cancelBtnImageStyle: {
      position:'absolute',
      right:10,
      padding: 8,
      width: 40,
      height: 40,
    }

})

调用


 showMyinfo(){ 

      this.myinfo.alert()
    }


    render(){
        return (
            <View >
                <Nav MyBtnPress={()=>{this.showMyinfo()}}/>
 
                 <Myinfo ref={myinfo => this.myinfo = myinfo }
            closeInterval={0}
          onClose={(data) => this.onClose(data)}
          onCancel={(data) => this.onClose(data)}
          showCancel={true}
                 />




            </View>


        );


    }


  closeAlert() {
    this.myinfo.onClose()
  }
  onClose(data) {
    console.log(data)
  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值