iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種方法

方法一:

網(wǎng)上用的很多的一種,不多說,直接上代碼.

-(void)startTime{
    __block int timeout= 60; //倒計(jì)時(shí)時(shí)間
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒執(zhí)行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒計(jì)時(shí)結(jié)束,關(guān)閉
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.getIdentifyCodeBt setTitle:@"獲取驗(yàn)證碼" forState:UIControlStateNormal];
                self.getIdentifyCodeBt.userInteractionEnabled = YES;
                [self.getIdentifyCodeBt setTitleColor:THEME_RED forState:UIControlStateNormal];
                self.getIdentifyCodeBt.backgroundColor = [UIColor whiteColor];
                self.getIdentifyCodeBt.layer.borderColor = THEME_RED.CGColor;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.getIdentifyCodeBt setTitle:[NSString stringWithFormat:@"%zd秒后失效",timeout] forState:UIControlStateNormal];
                [self.getIdentifyCodeBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                self.getIdentifyCodeBt.backgroundColor = [UIColor lightGrayColor];
                self.getIdentifyCodeBt.layer.borderColor = [UIColor clearColor].CGColor;
                self.getIdentifyCodeBt.clipsToBounds = YES;
                [UIView commitAnimations];
                self.getIdentifyCodeBt.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
    
}```
到時(shí)直接調(diào)用就可以了。
####方法二:利用分類
給UIButton新建一個(gè)分類
.h文件如下

import <UIKit/UIKit.h>

@interface UIButton (XSCountDown)

  • (void)xs_beginCountDownWithDuration:(NSTimeInterval)duration;
  • (void)xs_stopCountDown;
    @end
.m文件如下

import "UIButton+XSCountDown.h"

import "ThemeColor.h"

static NSTimer *_countTimer;
static NSTimeInterval _count;
static NSString *_title;

@implementation UIButton (XSCountDown)

  • (void)xs_beginCountDownWithDuration:(NSTimeInterval)duration {
    _title = self.titleLabel.text;
    _count = duration;
    _countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(xs_updateTitle) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_countTimer forMode:NSRunLoopCommonModes];
    self.userInteractionEnabled = NO;

    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.backgroundColor = [UIColor lightGrayColor];
    self.layer.borderColor = [UIColor clearColor].CGColor;
    self.clipsToBounds = YES;
    }

  • (void)xs_stopCountDown {
    [_countTimer invalidate];
    _countTimer = nil;
    _count = 60.0;
    [self setTitle:_title forState:UIControlStateNormal];
    self.userInteractionEnabled = YES;
    }

  • (void)xs_updateTitle {
    NSString *countString = [NSString stringWithFormat:@"%lis 后失效", (long)_count - 1];
    self.userInteractionEnabled = NO;
    [self setTitle:countString forState:UIControlStateNormal];
    if (_count-- <= 1.0) {
    [self xs_stopCountDown];
    [self setTitleColor:THEME_RED forState:UIControlStateNormal];
    self.backgroundColor = [UIColor whiteColor];
    self.layer.borderColor = THEME_RED.CGColor;
    }

}

@end

然后在controller里直接調(diào)用分類.h文件里的方法就ok了

[self.verifyBt xs_beginCountDownWithDuration:60.0];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *面試心聲:其實(shí)這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,595評論 30 472
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評論 19 139
  • 這兩周逢暑假得空回家,恰逢《楚喬傳》熱播。我自己已是楚喬的忠實(shí)粉絲了——獨(dú)立,堅(jiān)強(qiáng),自信,善良,有擔(dān)當(dāng)。是我完全認(rèn)...
    蹦跶的左小靜閱讀 334評論 0 1

友情鏈接更多精彩內(nèi)容