转载【React Native代码】手写验证码倒计时组件
实例代码:
import React, { Component , PropTypes} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity
} from 'react-native';
const DownButtonState = {
DownButtonActive : 0,
DownButtonDisable : 1,
}
// {id , startTime, deathCount}
var timeRecodes = []; //根据id来记录LCCountDownButton的信息
export default class DownButton extends Component {
// 构造
constructor(props) {
super(props);
// 初始状态
this.state={
btnTitle:'默认'
}
}
// static propTypes = {
// id:React.PropTypes.string, //按钮的身份标识,同一个页面的按钮是同一个id
// beginText:React.PropTypes.string, //初始状态按钮title
// endText:React.PropTypes.string, //读秒结束后按钮的title
// count:React.PropTypes.number, //计时数
// pressAction:React.PropTypes.func, //按下按钮的事件,但是触发倒数需要你自己来调用方法
// changeWithCount:React.PropTypes.func, //读秒变化的函数,该函数带有一个参数count,表示当前的剩余事件
// end:React.PropTypes.func, //读秒完毕后的函数
// frameStyle:View.propTypes.style //初始化的位置大小
// }
buttonState = DownButtonState.DownButtonActive;
componentWillMount() {
this.shouldSetState = true;
this.state = {
btnTitle:this.props.beginText,
}
}
componentDidMount() {
const {id,changeWithCount} = this.props;
for(var i = 0 ; i<timeRecodes.length ; i ++){
let obj = timeRecodes[i];
if (obj.id === id){
let liveTime = Date.now() - obj.startTime
if (liveTime < obj.deathCount * 1000){
//避免闪动
let detalTime = Math.round(liveTime/1000);
let content = changeWithCount(obj.deathCount - detalTime);
this.setState({
btnTitle:content
});
//手动调用倒计时
this.startCountDownWithCount(obj.startTime)
}
}
}
}
clearTime(){
if (this.interval){
clearInterval(this.interval)
}
}
componentWillUnmount() {
this.shouldSetState = false;
this.clearTime();
}
startCountDownWithCount(startTime){
this.buttonState = DownButtonState.DownButtonDisable;
const {changeWithCount,endText,count,end}= this.props;
this.startTime = startTime;
this.interval = setInterval(()=>{
let detalTime = Math.round((Date.now() - this.startTime)/1000);
let content = changeWithCount(count - detalTime);
if (detalTime >= count){
content = endText;
this.clearTime();
end && end();
this.buttonState = DownButtonState.DownButtonActive;
}
if(this.shouldSetState){
this.setState({
btnTitle:content
})
}
},1000)
}
recordButtonInfo(){
const {id , count} = this.props;
var hasRecord = false;
for (var i = 0 ; i < timeRecodes.length ; i ++){
let obj = timeRecodes[i];
if(obj.id === id){
obj.startTime = Date.now();
hasRecord = true;
break;
}
}
if (!hasRecord){
let buttonInfo = {
id:id,
deathCount:count,
startTime:Date.now()
}
timeRecodes.push(buttonInfo)
}
}
//外界调用
startCountDown(){
this.startCountDownWithCount(Date.now());
this.recordButtonInfo();
}
render(){
let isDisable = this.buttonState === DownButtonState.DownButtonDisable;
const {frameStyle} = this.props
return (
<TouchableOpacity disabled={isDisable}
onPress={()=>{this.props.pressAction && this.props.pressAction()}}
style={[styles.buttonCommonStyle,isDisable?styles.disableButtonStyle:styles.activeButtonStyle,frameStyle]}
>
<Text style={[styles.txtCommonStyle,isDisable?styles.disableTxtStyle:styles.activeTxtStyle]}>
{this.state.btnTitle}
</Text>
</TouchableOpacity>
);
}
}
const styles = StyleSheet.create({
buttonCommonStyle:{
paddingRight:8,
paddingLeft:8,
paddingTop:8,
paddingBottom:8,
justifyContent:'center',
alignItems:'center'
},
//禁用时候的TouchableOpacity样式
disableButtonStyle:{
// backgroundColor:'red',
},
//可以点击时候的TouchableOpacity样式
activeButtonStyle:{
// backgroundColor:'green',
},
txtCommonStyle:{
fontSize:14,
},
//禁用时候的Text样式
disableTxtStyle:{
color:'#7189D0',
},
//可以点击时候的Text样式
activeTxtStyle:{
color:'#767d72',
}
});
使用:
<DownButton
beginText='获取验证码'
endText='再次获取验证码'
count={60}
pressAction={()=>{this.onPressGetCode()}}
changeWithCount={(count)=> count + 's后重新获取'}
id='register'
ref={(e)=>{this.countDownButton=e}}
/>
代码源:https://www.cnblogs.com/pengsi/p/7681961.html
转载【React Native代码】手写验证码倒计时组件的更多相关文章
- 【Xamarin挖墙脚系列:代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧(转)】
正愁如何选择构建项目中的视图呢,现在官方推荐画板 Storybord...但是好像 xib貌似更胜一筹.以前的老棒子总喜欢装吊,用代码写....用代码堆一个HTML页面不知道你们尝试过没有.等页面做出 ...
- 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
近期接触了几个刚入门的iOS学习者,他们之中存在一个普遍和困惑和疑问.就是应该怎样制作UI界面.iOS应用是非常重视用户体验的,能够说绝大多数的应用成功与否与交互设计以及UI是否美丽易用有着非常大的关 ...
- 关于代码手写UI,xib和StoryBoard
代码手写UI 这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用.Geek们喜欢用代码构建UI,是因为代码是键盘敲出来的,这样可以做到不开IB,手不离开键盘就完成工作,可以专注于编码环境, ...
- 几道JS代码手写面试题
几道JS代码手写面试题 (1) 高阶段函数实现AOP(面向切面编程) Function.prototype.before = function (beforefn) { let ...
- [RN] React Native 好用的时间线 组件
React Native 好用的时间线 组件 效果如下: 实现方法: 一.组件封装 CustomTimeLine.js "use strict"; import React, {C ...
- Vue3语法快速入门以及写一个倒计时组件
Vue3写一个倒计时组件 vue3 beta版本发布已有一段时间了,文档也大概看了一下,不过对于学一门技术,最好的方法还是实战,于是找了一个比较简单的组件用vue3来实现,参考的是vant的count ...
- 基于React Native的Material Design风格的组件库 MRN
基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...
- 深入浅出React Native 3: 从零开始写一个Hello World
这是深入浅出React Native的第三篇文章. 1. 环境配置 2. 我的第一个应用 将index.ios.js中的代码全部删掉,为什么要删掉呢?因为我们准备从零开始写一个应用~学习技术最好的方式 ...
- [React Native]高度自增长的TextInput组件
之前我们学习了从零学React Native之11 TextInput了解了TextInput相关的属性. 在开发中,我们有时候有这样的需求, 希望输入区域的高度随着输入内容的长度而增长, 如下: 这 ...
随机推荐
- python从excel中读取数据传给其他函数使用
首先安装xlrd库 pip install xlrd 方法1: 表格内容如下: 场景描述,读取该表格A列数据,然后打印出数据 代码何解析如下: import xlrd #引入xlrd库 def exc ...
- AVR单片机教程——矩阵键盘
本文隶属于AVR单片机教程系列. 开发板上有4个按键,我们可以把每一个按键连接到一个单片机引脚上,来实现按键状态的检测.但是常见的键盘有104键,是每一个键分别连接到一个引脚上的吗?我没有考证过, ...
- rtmp协议规范
译序: 本文是为截至发稿时止最新 Adobe 官方公布的 RTMP 规范.本文包含 RTMP 规范的全部内容.是第一个比较全面的 RTMP 规范的中译本.由于成文时间仓促,加上作者知识面所限,翻译错误 ...
- mybatis 源码分析中的知识点
1. resultMap 和 resultType 之间的优劣 resultMap: 在联合查询的时候, 可以不用写Join (因为在resultMap 的定义里面已经写了这些东西了<asso ...
- 使用visual studio 2013读取.mat文件
现在有一个T.mat 文件需要在c++中处理然后以.mat 或是.txt形式返回 T.mat中存储了十个cell,每个cell中会有一个不等长的数组 1.以下是相关配置过程: 参考:http://we ...
- java.io 包下的类有哪些 + 面试题
java.io 包下的类有哪些 + 面试题 IO 介绍 IO 是 Input/Output 的缩写,它是基于流模型实现的,比如操作文件时使用输入流和输出流来写入和读取文件等. IO 分类 传统的 IO ...
- 并发队列之ConcurrentLinkedQueue
本来想着直接说线程池的,不过在说线程池之前,我们必须要知道并发安全队列:因为一般情况下线程池中的线程数量是一定的,肯定不会超过某个阈值,那么当任务太多了的时候,我们必须把多余的任务保存到并发安全队列中 ...
- 六、Django学习之基于下划线的跨表查询
六.Django学习之基于下划线的跨表查询 一对一 正向查询的例子为 已知用户名,查询用户的电话号码.反向查询例子反之. 正向查询 其中下划线前的表示表名,无下划线的表示的是Author表 resul ...
- HDU_2446_打表
http://acm.hdu.edu.cn/showproblem.php?pid=2446 打表,二分查找,注意查找最后的判断. #include<cstdio> #define N 2 ...
- 牛客练习赛25 A 因数个数和(数论分块)
题意: q次询问,每次给一个x,问1到x的因数个数的和. 1<=q<=10 ,1<= x<=10^9 1s 思路: 对1~n中的每个数i,i作为i,2i,3i,...的约数,一 ...