React Native使用Modal自定义分享界面的示例代码


Posted in Javascript onOctober 31, 2017

在很多App中都会涉及到分享,React Native提供了Modal组件用来实现一些模态弹窗,例如加载进度框,分享弹框等。使用Modal搭建分析的效果如下:

React Native使用Modal自定义分享界面的示例代码React Native使用Modal自定义分享界面的示例代码

自定义的分析界面代码如下:

ShareAlertDialog.js

/**
 * https://github.com/facebook/react-native
 * @flow 分享弹窗
 */


import React, {Component} from 'react';
import {View, TouchableOpacity, Alert,StyleSheet, Dimensions, Modal, Text, Image} from 'react-native';
import Separator from "./Separator";

const {width, height} = Dimensions.get('window');
const dialogH = 110;

export default class ShareAlertDialog extends Component {

  constructor(props) {
    super(props);
    this.state = {
      isVisible: this.props.show,
    };
  }

  componentWillReceiveProps(nextProps) {
    this.setState({isVisible: nextProps.show});
  }

  closeModal() {
    this.setState({
      isVisible: false
    });
    this.props.closeModal(false);
  }

  renderDialog() {
    return (
      <View style={styles.modalStyle}>
        <Text style={styles.text}>选择分享方式</Text>
        <Separator/>
        <View style={{flex: 1, flexDirection: 'row', marginTop: 15}}>
          <TouchableOpacity style={styles.item} onPress={() => Alert.alert('分享到微信朋友圈')}>
            <Image resizeMode='contain' style={styles.image}
                source={require('../images/share_ic_friends.png')}/>
            <Text>微信朋友圈</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.item}>
            <Image resizeMode='contain' style={styles.image}
                source={require('../images/share_ic_weixin.png')}/>
            <Text>微信好友</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.item}>
            <Image resizeMode='contain' style={styles.image}
                source={require('../images/share_ic_weibo.png')}/>
            <Text>新浪微博</Text>
          </TouchableOpacity>
        </View>
      </View>
    )
  }

  render() {

    return (
      <View style={{flex: 1}}>
        <Modal
          transparent={true}
          visible={this.state.isVisible}
          animationType={'fade'}
          onRequestClose={() => this.closeModal()}>
          <TouchableOpacity style={styles.container} activeOpacity={1}
                   onPress={() => this.closeModal()}>
            {this.renderDialog()}
          </TouchableOpacity>
        </Modal>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'rgba(0, 0, 0, 0.5)',
  },
  modalStyle: {
    position: "absolute",
    top: height - 170,
    left: 0,
    width: width,
    height: dialogH,
    backgroundColor: '#ffffff'
  },
  subView: {
    width: width,
    height: dialogH,
    backgroundColor: '#ffffff'
  },
  text: {
    flex: 1,
    fontSize: 18,
    margin: 10,
    justifyContent: 'center',
    alignItems: 'center',
    alignSelf: 'center'
  },
  item: {
    width: width / 3,
    height: 100,
    alignItems: 'center',
    backgroundColor: '#ffffff'
  },
  image: {
    width: 60,
    height: 60,
    marginBottom: 8
  },
});

当点击某个按钮之后,就会弹出框,示例代码如下:

constructor(props) {
    super(props);
    this.state = {
      showSharePop: false,//分享弹窗,默认不显示
    }
  }


//省略

onSharePress() {
    this.setState({showSharePop: !this.state.showSharePop})
  }
//增加点击
<NavigationBar
          navigator={this.props.navigator}
          popEnabled={false}
          style={{backgroundColor: "transparent", position: "absolute", top: 0, width}}
          leftButton={ViewUtils.getLeftButton(() => this.props.navigator.pop())}
          rightButton={ViewUtils.getShareButton(() => this.onSharePress())}/>

//添加ShareAlertDialog自定义组件
<ShareAlertDialog show={this.state.showSharePop} closeModal={(show) => {
          this.setState({showSharePop: show})
        }} {...this.props}/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
TimergliderJS 一个基于jQuery的时间轴插件
Dec 07 Javascript
javascript获取URL参数与参数值的示例代码
Dec 20 Javascript
使用js画图之饼图
Jan 12 Javascript
JavaScript实现基于Cookie的存储类实例
Apr 10 Javascript
JS版元素周期表实现方法
Aug 05 Javascript
Bootstrap布局组件应用实例讲解
Feb 17 Javascript
js获取指定字符前/后的字符串简单实例
Oct 27 Javascript
十大热门的JavaScript框架和库
Mar 21 Javascript
React进阶学习之组件的解耦之道
Aug 07 Javascript
使用JS编写的随机抽取号码的小程序
Aug 11 Javascript
vue 利用路由守卫判断是否登录的方法
Sep 29 Javascript
jQuery模仿ToDoList实现简单的待办事项列表
Dec 30 jQuery
Bootstrap3.3.7导航栏下拉菜单鼠标滑过展开效果
Oct 31 #Javascript
使用nvm管理不同版本的node与npm的方法
Oct 31 #Javascript
javascript高级模块化require.js的具体使用方法
Oct 31 #Javascript
JS简单实现点击跳转登陆邮箱功能的方法
Oct 31 #Javascript
jQuery简单实现对数组去重及排序操作实例
Oct 31 #jQuery
Node.js学习教程之HTTP/2服务器推送【译】
Oct 31 #Javascript
详解Vue用自定义指令完成一个下拉菜单(select组件)
Oct 31 #Javascript
You might like
ubuntu下编译安装xcache for php5.3 的具体操作步骤
2013/06/18 PHP
合并ThinkPHP配置文件以消除代码冗余的实现方法
2014/07/22 PHP
php上传图片类及用法示例
2016/05/11 PHP
PHP的时间戳与具体时间转化的简单实现
2016/06/13 PHP
php7安装mongoDB扩展的方法分析
2017/08/02 PHP
PHP实现的XXTEA加密解密算法示例
2018/08/28 PHP
Laravel框架实现多数据库连接操作详解
2019/07/12 PHP
JavaScript 页面编码与浏览器类型判断代码
2010/06/03 Javascript
js特效,页面下雪的小例子
2013/06/17 Javascript
JavaScript中使用Object.create()创建对象介绍
2014/12/30 Javascript
js获取json元素数量的方法
2015/01/27 Javascript
Json按某个键的值进行排序
2016/12/22 Javascript
jQuery+pjax简单示例汇总
2017/04/21 jQuery
老生常谈angularjs中的$state.go
2017/04/24 Javascript
利用canvas实现的加载动画效果实例代码
2017/07/05 Javascript
Angular实现的自定义模糊查询、排序及三角箭头标注功能示例
2017/12/28 Javascript
Node.js操作系统OS模块用法分析
2019/01/04 Javascript
微信小程序之高德地图多点路线规划过程示例详解
2021/01/18 Javascript
Windows上使用virtualenv搭建Python+Flask开发环境
2016/06/07 Python
python爬取网页内容转换为PDF文件
2020/07/28 Python
Tensorflow中使用tfrecord方式读取数据的方法
2018/06/19 Python
基于python实现微信好友数据分析(简单)
2020/02/16 Python
Python写捕鱼达人的游戏实现
2020/03/31 Python
Python正则表达式高级使用方法汇总
2020/06/18 Python
浅谈Python xlwings 读取Excel文件的正确姿势
2021/02/26 Python
三星印度官网:Samsung印度
2019/08/03 全球购物
中国好声音华少广告词
2014/03/17 职场文书
支行行长竞聘演讲稿
2014/05/15 职场文书
经济国贸专业求职信
2014/06/18 职场文书
2015年学校工作总结范文
2015/04/20 职场文书
小学少先队工作总结2015
2015/05/26 职场文书
开国大典观后感
2015/06/04 职场文书
初中数学教学反思范文
2016/02/17 职场文书
如何理解python接口自动化之logging日志模块
2021/06/15 Python
C#连接ORACLE出现乱码问题的解决方法
2021/10/05 Oracle
windows11怎么查看自己安装的版本号? win11版本号的查看方法
2021/11/21 数码科技