组件 DrawerLayout - react-native-gesture-handler

文档来源:

这是一个替代RN中 DrawerLayoutAndroid 的跨平台组件

使用 (usage)

import DrawerLayout from 'react-native-gesture-handler/DrawerLayout';

属性 (Properties)

  • drawerType: 'front' | 'back' | 'slide', 默认值 front. 3种不同的效果
  • edgeWidth: {number} 允许定义手势应该激活的内容视图边缘的距离
  • hideStatusBar: {boolean} 打开drawer时是否隐藏状态栏
  • statusBarAnimation'slide' | 'none' | 'fade',默认值 slide. 当 hideStatusBar设置为true时使用
  • overlayColor:遮罩背景色,默认是 black,颜色值需要是一个不包含alpha透明度的值,因为Drawer自身默认透明度从0%动画到70%
  • renderNavigationView: {function} 必需参数。这个函数接受一个 Animated Value 作为参数,用于显示打开或关闭drawer动画过程(当关闭时,progress为0;当打开时,progress为1). 这个可用于 drawer组件在打开或关闭时给其children添加动画效果
  • children: {Component | function} 默认是组件,直接被drawer包裹。也可以是一个函数,这个函数接受一个 Animated Value 作为参数,用于显示打开或关闭drawer动画过程(当关闭时,progress为0;当打开时,progress为1),这个和上面的 renderNavigationView一样

另外,其余的属性请参考 DrawerLayoutAndroid

示例

DrawerLayout 在RN应用中还是很常见的一种模式,这个例子基本上就是Drawer常见的使用方式

import React, { PureComponent } from 'react';
import { Platform, StyleSheet, Text, Animated, View, TextInput, Dimensions, SafeAreaView } from 'react-native';

import { RectButton } from 'react-native-gesture-handler';
import DrawerLayout from 'react-native-gesture-handler/DrawerLayout';

const TYPES = ['front', 'back', 'back', 'slide'];
const PARALLAX = [false, false, true, false];

const {width: DEVICE_WIDTH} = Dimensions.get('window');

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  page: {
    ...StyleSheet.absoluteFillObject,
    alignItems: 'center',
    paddingTop: 40,
    backgroundColor: 'gray',
  },
  pageText: {
    fontSize: 21,
    color: 'white',
  },
  rectButton: {
    height: 60,
    padding: 10,
    alignSelf: 'stretch',
    alignItems: 'center',
    justifyContent: 'center',
    marginTop: 20,
    backgroundColor: 'white',
  },
  rectButtonText: {
    backgroundColor: 'transparent',
  },
  drawerContainer: {
    flex: 1,
    paddingTop: 30,
  },
  pageInput: {
    height: 60,
    padding: 10,
    alignSelf: 'stretch',
    alignItems: 'center',
    justifyContent: 'center',
    marginTop: 20,
    backgroundColor: '#eee',
  },
  drawerText: {
    margin: 10,
    fontSize: 15,
    textAlign: 'left',
  },
});

// 被DrawerLayout包裹的页面
const Page = ({
  fromLeft, // 是否在左边打开Drawer
  type,
  parallaxOn, // 是否使用动画效果
  flipSide, // 切换Drawer所在位置 在左边还是右边
  nextType, // 切换 type 的值
  openDrawer,
}) => (
  <View style={styles.page}>
    <Text style={styles.pageText}>Hi DrawerLayout Demo</Text>
    <RectButton style={styles.rectButton} onPress={flipSide}>
      <Text>Drawer {fromLeft ? '在左边' : '在右边'}! -> 点击按钮切换</Text>
    </RectButton>
    <RectButton style={styles.rectButton} onPress={nextType}>
      <Text style={styles.rectButtonText}>
        抽屉类型 '{type}
        {parallaxOn && ' with parallax'}'! -> Next
      </Text>
    </RectButton>
    <RectButton style={styles.rectButton} onPress={openDrawer}>
      <Text style={styles.rectButtonText}>打开侧边栏</Text>
    </RectButton>
    <TextInput
      style={styles.pageInput}
      placeholder='使用拖动打开drawer时 键盘会自动收起'
    />
  </View>
);

export default class DrawerExample extends PureComponent {
  state = {
    fromLeft: true, // 是否在左边
    type: 0,
  };

  // 渲染动画效果
  // 使用 progressValue 动画值 进行插值操作
  renderParallaxDrawer = progressValue => {
    const parallax = progressValue.interpolate({
      inputRange: [0, 1],
      outputRange: [this.state.fromLeft ? -50 : 50, 0]
    });
    const animatedStyles = {
      transform: [
        { translateX: parallax },
      ],
    };

    return (
      <Animated.View
        style={[styles.drawerContainer, animatedStyles]}
      >
        <Text style={styles.drawerText}>侧边栏抽屉</Text>
        <Text style={styles.drawerText}>
          当拖动侧边栏抽屉时,观察parallax动画
        </Text>
      </Animated.View>
    )
  }


  // 普通的抽屉
  renderDrawer = () => {
    return (
      <View style={styles.drawerContainer}>
        <Text style={styles.drawerText}>这就是抽屉</Text>
      </View>
    );
  }

  render() {
    const drawerType = TYPES[this.state.type]; // ['front', 'back', 'back', 'slide'];
    const parallax = PARALLAX[this.state.type]; // 是否使用renderParallaxDrawer函数 [false, false, true, false] 和上面的drawerType相互对应
    // console.log('parallax, this.state.type', parallax, this.state.type)
    const position = this.state.fromLeft ? DrawerLayout.positions.Left : DrawerLayout.positions.Right; // 抽屉所在位置
    // const position = this.state.fromLeft ? 'left' : 'right'; // 或者写为这个样子也可以

    return (
      <View style={styles.container}>
        <DrawerLayout
          ref={drawer => {this.drawer = drawer}}
          drawerWidth={DEVICE_WIDTH - 50}
          drawerType={drawerType}
          keyboardDismissMode='on-drag'
          drawerPosition={position}
          drawerBackgroundColor='#4ae'
          overlayColor={drawerType === 'front' ? '#000' : '#6a1'}
          renderNavigationView={
            parallax ? this.renderParallaxDrawer : this.renderDrawer
          }
          contentContainerStyle={
            // 注意 drawerType 为 'front' 时,不要添加阴影效果,如果添加可能导致Drawer打不开
            drawerType === 'front'
              ? {}
              : Platform.select({
                ios: {
                  shadowColor: '#000',
                  shadowOpacity: 0.5,
                  shadowOffset: { width: 0, height: 2 },
                  shadowRadius: 60,
                },
                android: {
                  elevation: 100,
                  backgroundColor: '#000',
                },
              })
          }
        >
          <Page
            type={drawerType}
            fromLeft={this.state.fromLeft}
            parallaxOn={parallax}
            flipSide={() => this.setState({ fromLeft: !this.state.fromLeft })}
            nextType={() =>
              this.setState({ type: (this.state.type + 1) % TYPES.length })}
            openDrawer={() => this.drawer.openDrawer()}
          />
        </DrawerLayout>
      </View>
    )
  }
}

2019年04月03日22:08:16

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,928评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,748评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,282评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,065评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,101评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,855评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,521评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,414评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,931评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,053评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,191评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,873评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,529评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,074评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,188评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,491评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,173评论 2 357

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,112评论 4 61
  • ANavigator稍微复杂点的移动应用都会有页面跳转的场景,即用户在页面A上点击某个功能,比如查看内容详情或者帮...
    conf1234阅读 1,468评论 0 3
  • 时间像是可以停滞一般,在这近三个月,几乎没有让我操心的事情,懒散的放纵自己去无所事事,终于厌倦了这种状态,...
    背上背包就旅行21阅读 193评论 0 0
  • 《我的儿子》 1.上午去鸟市给啁啁买饲料,顺便问了鸟老板一个问题,即我对我家啁啁最近不爱唱歌表示了担忧。老板说天太...
    卿大小姐阅读 295评论 0 2
  • 1 助推和禀赋 1.1 内在的目标积极明确,外在的表现极其模糊。以一种看似散漫,实际是积极的超常主动性去影响他人行...
    呆呆LI阅读 667评论 0 50