React Native实现进度条弹框的示例代码


Posted in Javascript onJuly 17, 2017

本文介绍了React Native实现进度条弹框,分享给大家

我们在上传或者下载文件时候,希望有一个进度条弹框去提醒用户取当前正在上传或者下载,也允许用去取点击取消上传或者下载。

首先实现进度条。

import React, {
  PureComponent
} from 'react';
import {
  StyleSheet,
  View,
  Animated,
  Easing,
} from 'react-native';

class Bar extends PureComponent {

  constructor(props) {
    super(props);
    this.progress = new Animated.Value(this.props.initialProgress || 0);
  }

  static defaultProps = {
    style: styles,
    easing: Easing.inOut(Easing.ease)
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.progress >= 0 && this.props.progress !== nextProps.progress) {
      this.update(nextProps.progress);
    }
  }

  shouldComponentUpdate() {
    return false;
  }

  update(progress) {
    Animated.spring(this.progress, {
      toValue: progress
    }).start();
  }

  render() {
    return (
      <View style={[styles.background, this.props.backgroundStyle, this.props.style]}>
        <Animated.View style={[styles.fill, this.props.fillStyle, { width: this.progress.interpolate({
          inputRange: [0, 100],
          outputRange: [0 * this.props.style.width, 1 * this.props.style.width],
          })} ]}
        />
      </View>
    );
  }
}

var styles = StyleSheet.create({
  background: {
    backgroundColor: '#bbbbbb',
    height: 5,
    overflow: 'hidden'
  },
  fill: {
    backgroundColor: 'rgba(0, 122, 255, 1)',
    height: 5
  }
});

export default Bar;

进度条的值我们用动画实现,避免使用state不断去重新render渲染界面,同时设置shouldComponentUpdate返回值为false,避免重新render。

实现进度条弹框。

import React, {
  PropTypes,
  PureComponent
} from 'react';
import {
  View,
  StyleSheet,
  Modal,
  Text,
  Dimensions,
  TouchableOpacity
} from 'react-native';
import Bar from './Bar';

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

class ProgressBarDialog extends PureComponent {

  constructor(props) {
    super(props);
  }

  static propTypes = {
    ...Modal.propTypes,
    title: PropTypes.string,
    canclePress: PropTypes.func,
    cancleText: PropTypes.string,
    needCancle: PropTypes.bool
  };

  static defaultProps = {
    animationType: 'none',
    transparent: true,
    progressModalVisible: false,
    onShow: () => {},
    onRequestClose: () => {},
    onOutSidePress: () => {},
    title: '上传文件',
    cancleText: '取消是',
    canclePress: () => {},
    needCancle: true
  }

  render() {
    const {
      animationType,
      transparent,
      onRequestClose,
      progress,
      title,
      canclePress,
      cancleText,
      needCancle,
      progressModalVisible
    } = this.props;
    return (
      <Modal
        animationType={animationType}
        transparent={transparent}
        visible={progressModalVisible}
        onRequestClose={onRequestClose}>
        <View style={styles.progressBarView}>
          <View style={styles.subView}>
            <Text style={styles.title}>
              {title}
            </Text>
            <Bar
              ref={this.refBar}
              style={{marginLeft: 10,marginRight: 10,width:width - 80}}
              progress={progress}
              backgroundStyle={styles.barBackgroundStyle}
            />
            <View style={styles.progressContainer}>
              <Text style={styles.progressLeftText}>
                {`${progress}`}%
              </Text>
              <Text style={styles.progressRightText}>
                {`${progress}`}/100
              </Text>
            </View>
            {needCancle &&
              <View style={styles.cancleContainer}>
                <TouchableOpacity style={styles.cancleButton} onPress={canclePress}>
                  <Text style={styles.cancleText}>
                    {cancleText}
                  </Text>
                </TouchableOpacity>
              </View>
            }
          </View>
        </View>
      </Modal>
    );
  }
}

const styles = StyleSheet.create({
  progressBarView: {
    flex:1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'rgba(0,0,0,0.2)'
  },
  barStyle: {
    marginLeft: 10,
    marginRight: 10,
    width:width - 80
  },
  progressLeftText: {
    fontSize: 14
  },
  cancleContainer: {
    justifyContent: 'center',
    alignItems: 'flex-end'
  },
  progressRightText: {
    fontSize: 14,
    color: '#666666'
  },
  barBackgroundStyle: {
    backgroundColor: '#cccccc'
  },
  progressContainer: {
    flexDirection: 'row',
    padding: 10,
    justifyContent: 'space-between'
  },
  subView: {
    marginLeft: 30,
    marginRight: 30,
    backgroundColor: '#fff',
    alignSelf: 'stretch',
    justifyContent: 'center'
  },
  progressView: {
    flexDirection: 'row',
    padding: 10,
    paddingBottom: 5,
    justifyContent: 'space-between'
  },
  title: {
    textAlign: 'left',
    padding:10,
    fontSize: 16
  },
  cancleButton: {
    padding:10
  },
  cancleText: {
    textAlign: 'right',
    paddingTop: 0,
    fontSize: 16,
    color: 'rgba(0, 122, 255, 1)'
  }
});

export default ProgressBarDialog;

props

  1. modal的props - 这些都是modal的属性
    1. animationType - 动画类型
    2. transparent - 是否透明
    3. progressModalVisible - 是否可见
    4. onShow - 弹框出现
    5. onRequestClose - 弹框请求消失(比如安卓按返回键会触发这个方法)
  2. onOutSidePress - 点击弹框外部动作
  3. title - 弹框的标题
  4. cancleText - 取消的文字
  5. canclePress - 取消动作
  6. needCancle - 是否需要取消按钮

使用代码

import React , {
  PureComponent
} from 'react';
import {
  View
} from 'react-native';

import ProgressBarDialog from './ProgressBarDialog';

class Upload extends PureComponent {

  constructor(props) {
    this.state = {
      progress: 0,
      progressModalVisible: false
    };
  }

  refProgressBar = (view) => {
    this.progressBar = view;
  }

  showProgressBar = () => {
    this.setState({
      progressModalVisible: true
    });
  }

  dismissProgressBar = () => {
    this.setState({
      progress: 0,
      progressModalVisible: false
    });
  }

  setProgressValue = (progress) => {
    this.setState({
      progress
    });
  }

  onProgressRequestClose = () => {
    this.dismissProgressBar();
  }

  canclePress = () => {
    this.dismissProgressBar();
  }

  render() {
    return (
      <View>
        <ProgressBarDialog
          ref={this.refProgressBar}
          progress={this.state.progress}
          progressModalVisible={this.state.progressModalVisible}
          onRequestClose={this.onProgressRequestClose}
          canclePress={this.canclePress}
          needCancle={true}
        />
      </View>
    )
  }
}

export default Upload;

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

Javascript 相关文章推荐
Mootools 1.2教程 输入过滤第一部分(数字)
Sep 15 Javascript
JQuery事件e参数的方法preventDefault()取消默认行为
Sep 26 Javascript
javascript将相对路径转绝对路径示例
Mar 14 Javascript
js实现鼠标移到链接文字弹出一个提示层的方法
May 11 Javascript
jQuery实现的分子运动小球碰撞效果
Jan 27 Javascript
jQuery将表单序列化成一个Object对象的实例
Nov 29 Javascript
轻松理解JavaScript闭包
Mar 14 Javascript
详解Vue的computed(计算属性)使用实例之TodoList
Aug 07 Javascript
vue-router实现组件间的跳转(参数传递)
Nov 07 Javascript
基于axios 的responseType类型的设置方法
Oct 29 Javascript
Echarts实现单条折线可拖拽效果
Dec 19 Javascript
微信小程序用户授权最佳实践指南
May 08 Javascript
如何将 jQuery 从你的 Bootstrap 项目中移除(取而代之使用Vue.js)
Jul 17 #jQuery
Javascript ES6中对象类型Sets的介绍与使用详解
Jul 17 #Javascript
js实现图片懒加载效果
Jul 17 #Javascript
Vue.js项目部署到服务器的详细步骤
Jul 17 #Javascript
js图片放大镜实例讲解(必看篇)
Jul 17 #Javascript
js学习总结_轮播图之渐隐渐现版(实例讲解)
Jul 17 #Javascript
Vue 2.0在IE11中打开项目页面空白的问题解决
Jul 16 #Javascript
You might like
php将数据库中所有内容生成静态html文档的代码
2010/04/12 PHP
用PHP编写和读取XML的几种方式
2013/01/12 PHP
浅析linux下apache服务器的配置和管理
2013/08/10 PHP
php源码分析之DZX1.5加密解密函数authcode用法
2015/06/17 PHP
Yii隐藏URL中index.php的方法
2016/07/12 PHP
yii2.0数据库迁移教程【多个数据库同时同步数据】
2016/10/08 PHP
jquery选择器(常用选择器说明)
2010/09/28 Javascript
js实现基于正则表达式的轻量提示插件
2015/08/29 Javascript
node-http-proxy修改响应结果实例代码
2016/06/06 Javascript
AngularJS入门教程之迭代器过滤详解
2016/08/18 Javascript
Boostrap实现的登录界面实例代码
2016/10/09 Javascript
BootStrap入门学习第一篇
2017/08/28 Javascript
NodeJS设计模式总结【单例模式,适配器模式,装饰模式,观察者模式】
2017/09/06 NodeJs
nodejs实现截取上传视频中一帧作为预览图片
2017/12/10 NodeJs
关于js的三种使用方式(行内js、内部js、外部js)的程序代码
2018/05/05 Javascript
electron中使用bootstrap的示例代码
2018/11/06 Javascript
Vue动画事件详解及过渡动画实例
2019/02/09 Javascript
微信小程序--特定区域滚动到顶部时固定的方法
2019/04/28 Javascript
Vue Echarts实现可视化世界地图代码实例
2019/05/07 Javascript
Element InfiniteScroll无限滚动的具体使用方法
2020/07/27 Javascript
python中requests库session对象的妙用详解
2017/10/30 Python
Django实现全文检索的方法(支持中文)
2018/05/14 Python
Django 路由系统URLconf的使用
2018/10/11 Python
PyQT5 emit 和 connect的用法详解
2019/12/13 Python
详解用Python爬虫获取百度企业信用中企业基本信息
2020/07/02 Python
Python CategoricalDtype自定义排序实现原理解析
2020/09/11 Python
Python+unittest+requests 接口自动化测试框架搭建教程
2020/10/09 Python
python 爬虫爬取京东ps4售卖情况
2020/12/18 Python
Python3利用openpyxl读写Excel文件的方法实例
2021/02/03 Python
使用HTML5 Canvas绘制直线或折线等线条的方法讲解
2016/03/14 HTML / CSS
创造美妙香氛体验:Aera扩散器和香水
2018/11/25 全球购物
社团文化节邀请函
2014/01/10 职场文书
农村婚礼主持词
2014/03/13 职场文书
2014年社区党建工作汇报材料
2014/11/02 职场文书
质量整改通知单
2015/04/21 职场文书
应届生个人的求职(自荐信范文2篇)
2019/08/23 职场文书