ReactNative短信验证码倒计时控件

作者: 关玮琳linSir | 来源:发表于2017-06-29 11:52 被阅读509次

ReactNative短信验证码倒计时控件

功能

根据项目的需要,需要写一个自定义的控件,实现如下功能:

  • 默认文字为点击获取验证码
  • 点击后出现60秒的倒计时
  • 颜色,字号可调
  • 倒计时过程中,再次点击需要忽略掉
  • 倒计时完成后文本恢复成点击获取验证码
  • 再几次点击同之前

其实说了这么多,就是个最普通的验证码的功能。。。

效果

效果图如下:(录的图片比较一般,对付着看吧)

实现原理

自己封装了个控件,它内部含有一个Text控件,然后我们又写了一个timer,然后负责倒计时,然后每次都需要判断一下是否继续,然后加了一个flag字段,判断是否接受下次点击事件,当倒计时结束之后还需要将初始状态重置回去即可。

代码

控件代码

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>
        )
    }
}

应用代码

<MyCountTime timeLeft={5}>

</MyCountTime>

当然这只是,最简单的应用的代码,我们还提供了很多的自定义的属性,大家可以根据自己项目的需要,去调节这些参数。


由于最近刚开始认真的搞RN,可能有一些封装的不是最佳实践,还是希望大家多提意见,和大家一起进步吧。

相关文章

  • ReactNative短信验证码倒计时控件

    ReactNative短信验证码倒计时控件 功能 根据项目的需要,需要写一个自定义的控件,实现如下功能: 默认文字...

  • iOS-UIButton倒计时

    一般倒计时的使用场景就两种:发送短信验证码倒计时广告页倒计时 一、发送短信验证码倒计时 这种情况下,正在倒计时的按...

  • 代码整理

    短信验证码加倒计时:public function sendmess(){require('Public/Send...

  • IOS倒计时按钮实现思路及Demo

    前言 最近因为项目中涉及到短信获取验证码功能,并且获取验证码按钮需要显示倒计时功能,同时当从获取验证码界面在倒计时...

  • 微信小程序 短信验证码

    短信验证码 倒计时90秒 归0后恢复 获取验证码 1.login.js 2.login.wxml

  • 短信验证码为何要设置读秒

    短信验证码一般是1分钟倒计时,倒计时结束可重新发送短信,倒计时的含义是告诉用户等待,不要离开或者进行其他操作,是一...

  • ios开发 倒计时Button实现(一)

    这个文章主要是针对 短信验证码 倒计时 思路如下:说到倒计时无非想到的是NSTimer倒计时,在Button触发点...

  • jquery 手机短信倒计时

    手机短信验证码倒计时,必须用button或者用input的type='button' 例如

  • 链式调用

    这个是, 短信验证码的发送接口, 然后,验证码发送成功调用倒计时函数,不成功不调用倒计时,我一开始的想法,是返回一...

  • jquery短信验证码倒计时效果

    1. 点击发送短信验证码 按钮点击后禁用及倒计时60s

网友评论

  • dora_5b26:大神,有没有开源的网址呢

本文标题:ReactNative短信验证码倒计时控件

本文链接:https://www.haomeiwen.com/subject/hcsmcxtx.html