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 相关文章推荐
通过JAVASCRIPT读取ASP设定的COOKIE
Nov 24 Javascript
ExtJS 简介 让你知道extjs是什么
Dec 29 Javascript
jQuery技巧总结
Jan 01 Javascript
extjs实现选择多表自定义查询功能 前台部分(ext源码)
Dec 20 Javascript
jquery Moblie入门—hello world的示例代码学习
Jan 08 Javascript
Windows 系统下安装和部署Egret的开发环境
Jul 31 Javascript
JS/Jquery判断对象为空的方法
Jun 11 Javascript
javascript中的正则表达式使用详解
Aug 30 Javascript
基于JavaScript代码实现pc与手机之间的跳转
Dec 23 Javascript
微信小程序实现自定义加载图标功能
Jul 19 Javascript
js+html实现周岁年龄计算器
Jun 25 Javascript
vue使用原生swiper代码实例
Feb 05 Javascript
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
PHP编程最快明白(第一讲 软件环境和准备工作)
2010/10/25 PHP
解析web文件操作常见安全漏洞(目录、文件名检测漏洞)
2013/06/29 PHP
PHP防止表单重复提交的几种常用方法汇总
2014/08/19 PHP
10个超级有用值得收藏的PHP代码片段
2015/01/22 PHP
php利用smtp类实现电子邮件发送
2015/10/30 PHP
PHP查询附近的人及其距离的实现方法
2016/05/11 PHP
ThinkPHP框架整合微信支付之刷卡模式图文详解
2019/04/10 PHP
Laravel配置全局公共函数的方法步骤
2019/05/09 PHP
不常用但很实用的PHP预定义变量分析
2019/06/25 PHP
Thinkphp5.0框架视图view的循环标签用法示例
2019/10/12 PHP
CSS图片响应式 垂直水平居中
2015/08/14 Javascript
node.js 中国天气预报 简单实现
2016/06/06 Javascript
JavaScript中的await/async的作用和用法
2016/10/31 Javascript
常用原生js自定义函数总结
2016/11/20 Javascript
node.js中的事件处理机制详解
2016/11/26 Javascript
webpack多入口多出口的实现方法
2018/08/17 Javascript
angularjs获取到My97DatePicker选中的值方法
2018/10/02 Javascript
JSON基本语法及与JavaScript的异同实例分析
2019/01/04 Javascript
通过vue.extend实现消息提示弹框的方法记录
2021/01/07 Vue.js
[54:56]DOTA2上海特级锦标赛主赛事日 - 5 总决赛Liquid VS Secret第三局
2016/03/06 DOTA
[09:59]DOTA2-DPC中国联赛2月7日Recap集锦
2021/03/11 DOTA
Python3.2模拟实现webqq登录
2016/02/15 Python
浅谈Python2获取中文文件名的编码问题
2018/01/09 Python
解决Python2.7中IDLE启动没有反应的问题
2018/11/30 Python
PyQt5内嵌浏览器注入JavaScript脚本实现自动化操作的代码实例
2019/02/13 Python
python中matplotlib条件背景颜色的实现
2019/09/02 Python
浅谈Python的方法解析顺序(MRO)
2020/03/05 Python
Joe Fresh官网:加拿大时尚品牌和零售连锁店
2016/11/30 全球购物
巴黎卡诗美国官方网站:始于1964年的头发头皮护理专家
2017/07/10 全球购物
欧洲最大的滑雪假期供应商之一:Sunweb Holidays
2018/01/06 全球购物
电子商务自荐书范文
2014/01/04 职场文书
文秘求职信范文
2014/04/10 职场文书
小学教师师德师风自我剖析材料
2014/09/29 职场文书
2014报到证办理个人委托书
2014/10/08 职场文书
话题作文之关于呼唤
2019/11/29 职场文书
解决pycharm安装scrapy DLL load failed:找不到指定的程序的问题
2021/06/08 Python