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 相关文章推荐
地址栏上的一段语句,改变页面的风格。(教程)
Apr 02 Javascript
基于JQuery的数字改变的动画效果--可用来做计数器
Aug 11 Javascript
编写可维护面向对象的JavaScript代码[翻译]
Feb 12 Javascript
jQuery.autocomplete 支持中文输入(firefox)修正方法
Mar 10 Javascript
DOM和XMLHttpRequest对象的属性和方法整理
Jan 04 Javascript
深入理解JavaScript系列(12) 变量对象(Variable Object)
Jan 16 Javascript
js跳转页面方法实现汇总
Feb 11 Javascript
javascript中几个容易混淆的概念总结
Apr 14 Javascript
jquery实现简单实用的弹出层效果代码
Oct 15 Javascript
JavaScript中splice与slice的区别
May 09 Javascript
JS实现根据详细地址获取经纬度功能示例
Apr 16 Javascript
详解vuejs中执行npm run dev出现页面cannot GET/问题
Apr 26 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 smarty函数扩展
2010/03/15 PHP
PHP反射类ReflectionClass和ReflectionObject的使用方法
2013/11/13 PHP
phpstrom使用xdebug配置方法
2013/12/17 PHP
PHP添加Xdebug扩展的方法
2014/02/12 PHP
PHP获取POST数据的几种方法汇总
2015/03/03 PHP
PHP统一页面编码避免乱码问题
2015/04/09 PHP
jQuery 方法大全方便学习参考
2010/02/25 Javascript
判断一个对象是否为jquery对象的方法
2014/03/12 Javascript
javascript单引号和双引号的区别和处理
2014/05/14 Javascript
JavaScript中对循环语句的优化技巧深入探讨
2014/06/06 Javascript
当前流行的JavaScript代码风格指南
2014/09/10 Javascript
js实现拖拽功能
2017/03/01 Javascript
JavaScript数据结构之栈实例用法
2019/01/18 Javascript
Layer+Echarts构建弹出层折线图的方法
2019/09/25 Javascript
通过高德地图API获得某条道路上的所有坐标用于描绘道路的方法
2020/08/24 Javascript
python自定义异常实例详解
2017/07/11 Python
Python中property属性实例解析
2018/02/10 Python
Python爬豆瓣电影实例
2018/02/23 Python
python opencv旋转图像(保持图像不被裁减)
2018/07/26 Python
Python3中lambda表达式与函数式编程讲解
2019/01/14 Python
Pyqt5 关于流式布局和滚动条的综合使用示例代码
2020/03/24 Python
PyInstaller的安装和使用的详细步骤
2020/06/02 Python
DRF框架API版本管理实现方法解析
2020/08/21 Python
Python urllib request模块发送请求实现过程解析
2020/12/10 Python
欧洲最大的美妆零售网站:Feelunique
2017/01/14 全球购物
SneakerStudio英国:最佳运动鞋商店
2019/05/22 全球购物
年度考核自我鉴定
2013/11/09 职场文书
单位作风建设剖析材料
2014/10/11 职场文书
2014年预算员工作总结
2014/12/05 职场文书
病人慰问信范文
2015/02/15 职场文书
初一军训感言
2015/08/01 职场文书
机械生产实习心得体会
2016/01/22 职场文书
Python趣味爬虫之用Python实现智慧校园一键评教
2021/05/28 Python
Redis Cluster 集群搭建你会吗
2021/08/04 Redis
mysql分表之后如何平滑上线详解
2021/11/01 MySQL
阿里云国际版 使用Nginx作为HTTPS转发代理服务器
2022/05/11 Servers