React Native实现进度条弹框的示例代码
时间:2019-03-30
本文章向大家介绍React Native实现进度条弹框的示例代码,主要包括React Native实现进度条弹框的示例代码使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
本文介绍了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
- modal的props - 这些都是modal的属性
- animationType - 动画类型
- transparent - 是否透明
- progressModalVisible - 是否可见
- onShow - 弹框出现
- onRequestClose - 弹框请求消失(比如安卓按返回键会触发这个方法)
- onOutSidePress - 点击弹框外部动作
- title - 弹框的标题
- cancleText - 取消的文字
- canclePress - 取消动作
- 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;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
- Golang语言社区--Go语言基础第四节类型
- Golang语言社区--go语言编写Web程序
- Golang语言社区--Go语言基础第五节流程控制
- (14)不同基因坐标转换-生信菜鸟团博客2周年精选文章集
- (15)基因组各种版本对应关系-生信菜鸟团博客2周年精选文章集
- go 并发处理脚本
- 生信菜鸟团博客2周年精选文章集(4)NCBI数据库的几个探索
- PHP 的前世今生
- 【直播】我的基因组49:Y染色体的SNV不能用常规流程来找?
- 【直播】我的基因组46:SNV突变(96种)频谱的制作
- 深入剖析-Oracle索引分支块的结构
- 【直播】我的基因组48:我可能测了一个假的全基因组
- 【直播】我的基因组47:测序深度和GC含量的关系
- 【直播】我的基因组47:测序深度和GC含量的关系
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 容器化 FRP 使用方案
- 学习从拥有一支好笔开始
- leetcode栈之最小栈
- Discourse 安装防火墙的配置如何让邮件能够发送
- CentOS 8 如何安装 htop
- Discourse 重复安装过程中的密钥签发问题
- Appium之「元素定位和UiAutomator表达式」
- 机器人软件开发:机器人开源库安装
- 2020-10-05:如何求模平方根?
- leetcode栈之比较含退格的字符串
- Discourse 如何不使用 Let’s Encrypt 而使用 CA 签名的密钥进行安装
- 3分钟短文:Laravel slug,让你的url地址更“好记”
- Qt音视频开发28-Onvif信息获取
- CentOS 8 启用 NTP 服务
- Discourse 如何使用命令行方式进行恢复