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 相关文章推荐
15款优秀的jQuery导航菜单插件分享
Jul 19 Javascript
jQuery实现Meizu魅族官方网站的导航菜单效果
Sep 14 Javascript
js计算系统当前日期是星期几的方法
Jul 14 Javascript
js删除数组元素、清空数组的简单方法(必看)
Jul 27 Javascript
Vue中computed与methods的区别详解
Mar 24 Javascript
详解vuex之store拆分即多模块状态管理(modules)篇
Nov 13 Javascript
Vue中Table组件Select的勾选和取消勾选事件详解
Mar 19 Javascript
微信小程序传值以及获取值方法的详解
Apr 29 Javascript
jquery实现两个div中的元素相互拖动的方法分析
Apr 05 jQuery
简单了解three.js 着色器材质
Aug 03 Javascript
Vue为什么要谨慎使用$attrs与$listeners
Aug 27 Javascript
如何在VUE中使用vue-awesome-swiper
Jan 04 Vue.js
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 截取字符串 分别适合GB2312和UTF8编码情况
2009/02/12 PHP
54个提高PHP程序运行效率的方法
2015/07/19 PHP
php生成mysql的数据字典
2016/07/07 PHP
js封装的textarea操作方法集合(兼容很好)
2010/11/16 Javascript
node.js中的fs.read方法使用说明
2014/12/17 Javascript
js实现有时间限制消失的图片方法
2015/02/27 Javascript
js小数运算出现多位小数如何解决
2015/10/08 Javascript
js以及jquery实现手风琴效果
2020/04/17 Javascript
ES6学习之变量的两种命名方法示例
2017/07/18 Javascript
微信小程序中使用自定义图标(阿里icon)的方法
2018/08/20 Javascript
vue中使用heatmapjs的示例代码(结合百度地图)
2018/09/05 Javascript
详解KOA2如何手写中间件(装饰器模式)
2018/10/11 Javascript
微信小程序定义和调用全局变量globalData的实现
2019/11/01 Javascript
解决VUE-Router 同一页面第二次进入不刷新的问题
2020/07/22 Javascript
antd-日历组件,前后禁止选择,只能选中间一部分的实例
2020/10/29 Javascript
浅谈Python中用datetime包进行对时间的一些操作
2016/06/23 Python
python画一个玫瑰和一个爱心
2020/08/18 Python
详解向scrapy中的spider传递参数的几种方法(2种)
2020/09/28 Python
五种Python转义表示法
2020/11/27 Python
用Python 执行cmd命令
2020/12/18 Python
python 基于opencv实现图像增强
2020/12/23 Python
Parfume Klik丹麦:香水网上商店
2018/07/10 全球购物
如何写毕业求职自荐信
2013/11/06 职场文书
应届毕业生专业个人求职自荐信格式
2013/11/20 职场文书
考试作弊被抓检讨书
2014/01/10 职场文书
中文专业学生自我评价范文
2014/02/06 职场文书
部队万能检讨书
2014/02/20 职场文书
党的群众路线教育学习材料
2014/05/12 职场文书
护士找工作求职信
2014/07/02 职场文书
工程部文员岗位职责
2015/02/04 职场文书
2015年人事工作总结范文
2015/04/09 职场文书
公司处罚决定书
2015/06/24 职场文书
网吧员工管理制度
2015/08/05 职场文书
2016春季小学开学寄语
2015/12/03 职场文书
Python中Matplotlib的点、线形状、颜色以及绘制散点图
2022/04/07 Python
JS前端使用Canvas快速实现手势解锁特效
2022/09/23 Javascript