iOS獲取短信驗證碼倒計時的兩種實現(xiàn)方法
更新時間:2017年05月18日 11:00:42 作者:APP叫我取個帥氣的昵稱
本篇文章主要介紹了iOS獲取短信驗證碼倒計時的兩種實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
方法一:
網(wǎng)上用的很多的一種,不多說,直接上代碼.
-(void)startTime{ __block int timeout= 60; //倒計時時間 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){ //倒計時結(jié)束,關(guān)閉 dispatch_source_cancel(_timer); dispatch_async(dispatch_get_main_queue(), ^{ [self.getIdentifyCodeBt setTitle:@"獲取驗證碼" 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); }
到時直接調(diào)用就可以了。
方法二:利用分類
給UIButton新建一個分類
.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];
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS網(wǎng)絡(luò)請求之AFNetWorking 3.x 使用詳情
本篇文章主要介紹了IOS網(wǎng)絡(luò)請求之AFNetWorking 3.x 使用詳情,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02iOS App中數(shù)據(jù)管理框架Core Data的基本數(shù)據(jù)操作教程
Core Data框架能夠為我們提供比操作SQL關(guān)系型數(shù)據(jù)庫更簡單的數(shù)據(jù)管理方式,而且內(nèi)置于Xcode中配合IDE操作十分方便,下面我們就來看一下iOS App中數(shù)據(jù)管理框架Core Data的基本數(shù)據(jù)操作教程2016-06-06iOS開發(fā)中使用屏幕旋轉(zhuǎn)功能的相關(guān)方法
這篇文章主要介紹了iOS開發(fā)中使用屏幕旋轉(zhuǎn)功能的相關(guān)方法,包括Transform變化矩陣原理的講解,需要的朋友可以參考下2015-09-09