ReactNative短信驗證碼倒計時控件的實現(xiàn)代碼
由于最近剛開始認真的搞RN,可能有一些封裝的不是最佳實踐,還是希望大家多提意見,和大家一起進步吧。本文介紹了ReactNative短信驗證碼倒計時控件,分享給大家
功能
根據(jù)項目的需要,需要寫一個自定義的控件,實現(xiàn)如下功能:
- 默認文字為點擊獲取驗證碼
- 點擊后出現(xiàn)60秒的倒計時
- 顏色,字號可調(diào)
- 倒計時過程中,再次點擊需要忽略掉
- 倒計時完成后文本恢復(fù)成點擊獲取驗證碼
- 再幾次點擊同之前
其實說了這么多,就是個最普通的驗證碼的功能。。。
效果
效果圖如下:(錄的圖片比較一般,對付著看吧)

實現(xiàn)原理
自己封裝了個控件,它內(nèi)部含有一個Text控件,然后我們又寫了一個timer,然后負責倒計時,然后每次都需要判斷一下是否繼續(xù),然后加了一個flag字段,判斷是否接受下次點擊事件,當?shù)褂嫊r結(jié)束之后還需要將初始狀態(tài)重置回去即可。
代碼
控件代碼
import React, {Component } from 'react';
import {
StyleSheet,
Text,
View,
Image,
TextInput,
TouchableHighlight,
StatusBar,
Alert,
AppRegistry
} from 'react-native';
import LinkRow from '../components/LinkRow';
import cStyles from '../styles/CommonStyle';
import axios from 'axios';
class MyCountTime extends Component {
constructor(props) {
super(props);
let timeLeft = this.props.timeLeft > 0 ? this.props.timeLeft : 5;
let width = this.props.width || 100;
let height = this.props.height || 50;
let color = this.props.color || '#42A5F5';
let fontSize = this.props.fontSize || 30;
let fontWeight = this.props.fontWeight || '600';
let borderColor = this.props.borderColor || '#42A5F5';
let borderWidth = this.props.borderWidth || 1;
let borderRadius = this.props.borderRadius || 4;
let backgroundColor = this.props.backgroundColor || '#42A5F5';
let begin = 0;
let press = this.props.press;
this.afterEnd = this.props.afterEnd || this._afterEnd;
this.style = this.props.style;
this.state = {
timeLeft: timeLeft,
begin: begin
};
this.countTextStyle = {
textAlign: 'center',
color: '#42A5F5',
fontSize: fontSize,
fontWeight: fontWeight
};
this.countViewStyle = {
backgroundColor: backgroundColor,
alignItems: 'center',
borderColor: borderColor,
borderWidth: borderWidth,
borderRadius: borderRadius,
width: width,
height: height
}
}
countdownfn(timeLeft, callback, begin) {
if (timeLeft > 0) {
this.state.begin = 1;
console.log("===lin===>");
let that = this;
let interval = setInterval(function () {
if (that.state.timeLeft < 1) {
clearInterval(interval);
callback(that)
} else {
let totalTime = that.state.timeLeft;
that.setState({
timeLeft: totalTime - 1
})
}
}, 1000)
}
}
_beginCountDown() {
if (this.state.begin === 1){
return;
}
let time = this.state.timeLeft;
console.log("===lin===> time " + time);
let afterEnd = this.afterEnd;
let begin = this.state.begin;
console.log("===lin===> start " + begin);
this.countdownfn(time, afterEnd, begin)
}
_afterEnd(that) {
console.log('------------time over');
that.setState({
begin : 0,
timeLeft : 5,
})
}
componentDidMount() {
}
render() {
return (
<View style={{position:'absolute',top:13,right:43,height:30}}>
<Text
onPress={this._beginCountDown.bind(this)}
style={{color: '#42A5F5', fontSize: 17,height:40 , zIndex:999}}> { this.state.begin === 0 ? '點擊獲取驗證碼' : this.state.timeLeft} </Text>
</View>
)
}
}
應(yīng)用代碼
<MyCountTime timeLeft={5}>
</MyCountTime>
當然這只是,最簡單的應(yīng)用的代碼,我們還提供了很多的自定義的屬性,大家可以根據(jù)自己項目的需要,去調(diào)節(jié)這些參數(shù)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React實現(xiàn)單向數(shù)據(jù)流的方法
本文主要介紹了React實現(xiàn)單向數(shù)據(jù)流的方法2023-04-04
React/Redux應(yīng)用使用Async/Await的方法
本篇文章主要介紹了React/Redux應(yīng)用使用Async/Await的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

