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 Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解
下面小編就為大家分享一篇基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
iOS動(dòng)畫(huà)教你編寫(xiě)Slack的Loading動(dòng)畫(huà)進(jìn)階篇
這篇文章主要為大家進(jìn)一步詳細(xì)介紹了iOS動(dòng)畫(huà)教你編寫(xiě)Slack的Loading動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
iOS面試中如何優(yōu)雅回答B(yǎng)lock導(dǎo)致循環(huán)引用的問(wèn)題
這篇文章主要給大家介紹了iOS面試中關(guān)于如何優(yōu)雅回答B(yǎng)lock導(dǎo)致循環(huán)引用的問(wèn)題的相關(guān)資料,文中通過(guò)圖文介紹的非常相信,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03
iOS10 適配-Xcode8問(wèn)題總結(jié)及解決方案
這篇文章主要介紹了iOS10 適配-Xcode8問(wèn)題總結(jié)的相關(guān)資料,這里整理了遇到的幾種問(wèn)題,并給出解決方案,需要的朋友可以參考下2016-11-11
IOS開(kāi)發(fā)基礎(chǔ)之二維數(shù)組詳解
這篇文章主要介紹了IOS開(kāi)發(fā)基礎(chǔ)之二維數(shù)組詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

