实例代码:

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代码】手写验证码倒计时组件的更多相关文章

  1. 【Xamarin挖墙脚系列:代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧(转)】

    正愁如何选择构建项目中的视图呢,现在官方推荐画板 Storybord...但是好像 xib貌似更胜一筹.以前的老棒子总喜欢装吊,用代码写....用代码堆一个HTML页面不知道你们尝试过没有.等页面做出 ...

  2. 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧

    近期接触了几个刚入门的iOS学习者,他们之中存在一个普遍和困惑和疑问.就是应该怎样制作UI界面.iOS应用是非常重视用户体验的,能够说绝大多数的应用成功与否与交互设计以及UI是否美丽易用有着非常大的关 ...

  3. 关于代码手写UI,xib和StoryBoard

    代码手写UI 这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用.Geek们喜欢用代码构建UI,是因为代码是键盘敲出来的,这样可以做到不开IB,手不离开键盘就完成工作,可以专注于编码环境, ...

  4. 几道JS代码手写面试题

    几道JS代码手写面试题   (1) 高阶段函数实现AOP(面向切面编程)    Function.prototype.before = function (beforefn) {        let ...

  5. [RN] React Native 好用的时间线 组件

    React Native 好用的时间线 组件 效果如下: 实现方法: 一.组件封装 CustomTimeLine.js "use strict"; import React, {C ...

  6. Vue3语法快速入门以及写一个倒计时组件

    Vue3写一个倒计时组件 vue3 beta版本发布已有一段时间了,文档也大概看了一下,不过对于学一门技术,最好的方法还是实战,于是找了一个比较简单的组件用vue3来实现,参考的是vant的count ...

  7. 基于React Native的Material Design风格的组件库 MRN

    基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...

  8. 深入浅出React Native 3: 从零开始写一个Hello World

    这是深入浅出React Native的第三篇文章. 1. 环境配置 2. 我的第一个应用 将index.ios.js中的代码全部删掉,为什么要删掉呢?因为我们准备从零开始写一个应用~学习技术最好的方式 ...

  9. [React Native]高度自增长的TextInput组件

    之前我们学习了从零学React Native之11 TextInput了解了TextInput相关的属性. 在开发中,我们有时候有这样的需求, 希望输入区域的高度随着输入内容的长度而增长, 如下: 这 ...

随机推荐

  1. 《阿k学Python》一Python入门(一)

    前言 各位看博客的园友们,大家好,我就是那个风流倜傥的KK,还记得我那篇2019年的年中总结博客吗?我想有许多看博客的园友是没有读过我那篇文章的,KK很生气,后果很严重(开个玩笑了,怎么可能).给大家 ...

  2. Commvault Oracle备份常用命令

    在进行Oracle数据库备份的配置.发起和恢复的过程中,需要用到许多Oracle数据库本身的命令.在此章节中进行命令的梳理,供大家参考. Oracle用户和实例相关命令 Linux/Unix平台 # ...

  3. Linux 高压缩率工具 XZ 压缩详解

    目录 一.XZ 基础信息 二.安装 三.详解 3.1.常用的参数 3.2. 常用命令 四.扩展 4.1.unxz 4.2.xzcat 4.3.lzma 4.4.unlzma 4.5.lzcat 一.X ...

  4. ImportError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work.

    用了pip install pydot; pip install graphviz都不行 去网上查了才发现window下要去https://graphviz.gitlab.io/下载windows版本 ...

  5. 小白学 Python 数据分析(1):数据分析基础

    各位同学好,小编接下来为大家分享一些有关 Python 数据分析方面的内容,希望大家能够喜欢. 人工植入广告: PS:小编最近两天偷了点懒,好久没有发原创了,最近是在 CSDN 开通了一个付费专栏,用 ...

  6. sharding-JDBC学习笔记

    sharding-JDBC学习笔记 ShardingSphere ShardingSphere是一套开源的分布式数据库中间件解决方案组成的生态圈,它由Sharding-JDBC.Sharding-Pr ...

  7. QT5如何设置QLabel中字体的颜色

    修改了wd的文章: 如何使用Qt5,设置QLabel中字体的颜色. 大致有几种做法: 一是使用setPalette()方法: 二是使用样式表: 三是可以使用QStyle: 四是可以在其中使用一些简单的 ...

  8. python 类 - 继承

    继承 什么是继承? 编写类时,并非总要从空白开始.如果要编写的类是另一个现成类的特殊版本,可使用继承. 一个类继承另一个类时,将自动获得另一个类的所有属性和方法.现有的类称为父类,而新类称为子类. 子 ...

  9. jmeter 源码修改返回值中文Unicode编码问题

    修改jmeter源码,可能会对其他格式的responseData有一定影响,图片或者其他 在 ListenerNotifier 类中找到 notifyListeners 方法,在其下面添加如下代码: ...

  10. Go语言实现:【剑指offer】求1+2+3+...+n

    该题目来源于牛客网<剑指offer>专题. 求1+2+3+-+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). Go ...