实例代码:

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. java小心机(4)| 继承与组合的爱恨情仇

    在java中,有两种主要复用代码的方法:继承和组合. 继承,是OOP的一大特性,想必大家都非常熟悉了;组合,其实也很常见,只是不知道它的名字罢了. 继承 子类拥有父类的基本特性,需使用extend关键 ...

  2. Ogre3d 1.7.x 的 RTShaderSystem的一个BUG

    来源:http://www.ogre3d.org/forums/viewtopic.php?f=2&t=63644 表现:使用dx的shader可能会造成程序崩溃. 在文件OgreShader ...

  3. 异想家Win10系统安装的软件与配置

    1.C盘推荐一个硬盘,256G,安装好驱动,显卡配置好高性能,激活Win10,屏蔽WIn10驱动更新(Show or hide updates.diagcab),改电脑名称为Sandeepin-PC. ...

  4. Solution: 题解 CF1196E Connected Component on a Chessboard

    感觉这题还可以 因为总空间比输入数量 不知高到哪里去了 ,所以完全不需要考虑放不下的问题 从贪心的角度考虑,如果要使相差数量巨大的\(b\)和\(w\)能够成功放下来,应该使这些方块尽量分散(似乎有点 ...

  5. Linux 常用工具openssh之ssh-agent

    前言 ssh-agent命令是一种控制用来保存公钥身份验证所使用的私钥的程序.ssh-agent在X会话或登录会话之初启动,所有其他窗口或程序则以客户端程序的身份启动并加入到ssh-agent程序中. ...

  6. java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"

    问题描述 今天在使用SpringBoot整合spring security,使用内存用户验证,但无响应报错:java.lang.IllegalArgumentException: There is n ...

  7. <背包>solution-CF118D_Caesar's Legions

    Caesar's Legions Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the a ...

  8. 场景6:具有OpenvSwitch的提供商网络

    此场景描述了使用带有Open vSwitch(OVS)的ML2插件的OpenStack网络服务的提供者网络实现. 在OpenStack网络引入分布式虚拟路由器之前,所有网络通信都通过一个或多个专门的网 ...

  9. (二)maven依赖,两个项目之间如何依赖,继承实现

    maven的jar之间存在依赖关系的,我们在引入一个时,其他有依赖关系的也会被引入 依赖排除: 比如现在有两个依赖关系,A(x,java,y.java,z.java)  B(a,java,b,java ...

  10. 再次聚焦DOCKER MACHINE CODE 2048

    如果有一种feeling让世界难以释怀,那一定是发掘(挖土机那家强?)了什么了不起的东西 如果有一种贴图叫做深夜,仍不止息,那一定是饱含深意的贴图 // TODO: I'm not super hap ...