转载【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相关的属性. 在开发中,我们有时候有这样的需求, 希望输入区域的高度随着输入内容的长度而增长, 如下: 这 ...
随机推荐
- 【java面试】框架篇之Spring
1.你如何理解Spring? 具体来说Spring是一个轻量级的容器,用于管理业务相关对象的.核心功能主要为:IOC,AOP,MVC. IOD:控制反转,将对象的创建过程交给容器,让容器管理对象的生命 ...
- git使用的常见命令汇总
git的简单介绍 git是分布式版本控制工具 git 的基本操作指令 git init 初始化git仓库 git add 文件名 git add . 把文件 添加到 git 暂存区中 git stat ...
- sqli_labs学习笔记(一)Less-54~Less-65
续上,开门见山 暴库: http://43.247.91.228:84/Less-54/?id=-1' union select 1,2,database() --+ challenges 爆表: h ...
- Django 2.2
Django 2.2 LTS 发布,长期支持版来了 django中文网:https://www.django.cn/course/course-3.html Django 2.2 已正式发布,这是一个 ...
- 自定义BeanDefinitionRegistryPostProcessor注册bean
自定义BeanDefinitionRegistryPostProcessor 概述 BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProce ...
- nmap详解之基础概述
概述 nmap是一个网络探测和安全扫描程序,系统管理者和个人可以使用这个软件扫描大型的网络,获取那台主机正在运行以及提供什么服务等信息.nmap支持很多扫描技术,例如:UDP.TCP connect( ...
- SpringBoot高级篇Ⅸ --- 热部署与监控管理
一.热部署 在开发中我们修改一个Java文件后想看到效果不得不重启应用,这导致大量时间花费,我们不希望重启应用的情况下,程序可以自动部署(热部署). 1.1 模板引擎 在SpringBoot中开发情况 ...
- Java:线程的六种状态及转化
目录 Java:线程的六种状态及转化 一.新建状态(NEW) 二.运行状态(RUNNABLE) 就绪状态(READY) 运行状态(RUNNING) 三.阻塞状态(BLOCKED) 四.等待状态(WAI ...
- POJ 1789 Truck History (Kruskal最小生成树) 模板题
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- It is possible and safe to monitor a table DML history on sqlserver
He is my test step: In a test enviroument, I make a table "test"/ demo table:create table ...