iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法
方法一:
網(wǎng)上用的很多的一種,不多說(shuō),直接上代碼.
-(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)用就可以了。
方法二:利用分類(lèi)
給UIButton新建一個(gè)分類(lèi)
.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)用分類(lèi).h文件里的方法就ok了
[self.verifyBt xs_beginCountDownWithDuration:60.0];
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- IOS開(kāi)發(fā)代碼分享之用nstimer實(shí)現(xiàn)倒計(jì)時(shí)功能
- IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(一)
- IOS關(guān)于大型網(wǎng)站搶購(gòu)、距活動(dòng)結(jié)束,剩余時(shí)間倒計(jì)時(shí)的實(shí)現(xiàn)代碼
- ios 實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式
- iOS中實(shí)現(xiàn)簡(jiǎn)單易懂秒殺倒計(jì)時(shí)/倒計(jì)時(shí)代碼
- iOS中讓多個(gè)cell上都出現(xiàn)倒計(jì)時(shí)的分析與實(shí)現(xiàn)
- iOS實(shí)現(xiàn)毫秒倒計(jì)時(shí)的方法詳解
- iOS啟動(dòng)頁(yè)倒計(jì)時(shí)跳過(guò)按鈕功能
- Swift實(shí)現(xiàn)iOS應(yīng)用中短信驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)例分享
- iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí)
相關(guān)文章
IOS網(wǎng)絡(luò)請(qǐng)求之AFNetWorking 3.x 使用詳情
本篇文章主要介紹了IOS網(wǎng)絡(luò)請(qǐng)求之AFNetWorking 3.x 使用詳情,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02Swift 進(jìn)階 —— map 和 flatMap的使用
這篇文章主要介紹了Swift map和flatMap的相關(guān)資料,幫助大家更好的理解和使用Swift,感興趣的朋友可以了解下2020-09-09IOS開(kāi)發(fā)基礎(chǔ)之二維數(shù)組詳解
這篇文章主要介紹了IOS開(kāi)發(fā)基礎(chǔ)之二維數(shù)組詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04iOS App中數(shù)據(jù)管理框架Core Data的基本數(shù)據(jù)操作教程
Core Data框架能夠?yàn)槲覀兲峁┍炔僮鱏QL關(guān)系型數(shù)據(jù)庫(kù)更簡(jiǎn)單的數(shù)據(jù)管理方式,而且內(nèi)置于Xcode中配合IDE操作十分方便,下面我們就來(lái)看一下iOS App中數(shù)據(jù)管理框架Core Data的基本數(shù)據(jù)操作教程2016-06-06iOS開(kāi)發(fā)中使用屏幕旋轉(zhuǎn)功能的相關(guān)方法
這篇文章主要介紹了iOS開(kāi)發(fā)中使用屏幕旋轉(zhuǎn)功能的相關(guān)方法,包括Transform變化矩陣原理的講解,需要的朋友可以參考下2015-09-09iOS內(nèi)存管理中引用計(jì)數(shù)的學(xué)習(xí)
文章給大家分享了關(guān)于iOS內(nèi)存管理中引用計(jì)數(shù)的相關(guān)知識(shí)點(diǎn),對(duì)此有需要的朋友可以跟著學(xué)習(xí)下。2018-05-05