iOS 驗證碼按鈕倒計時功能
更新時間:2017年07月27日 09:09:30 作者:鴻鵠當高遠
在app注冊或者登錄需要驗證碼的地方、為了避免短時間內(nèi)刷驗證碼、往往會加上一層驗證當?shù)褂嫊r結(jié)束后、可以重新獲取,關(guān)于ios 驗證碼按鈕倒計時功能大家可以參考下本文
在app 注冊或者登錄 需要驗證碼的地方、為了避免短時間內(nèi)刷驗證碼、往往會加上一層驗證。

倒計時結(jié)束后、可以重新獲取!

代碼實現(xiàn)如下:
// _CountdownTime 倒計時總時間;
//_timer 定時器
- (void)startTime:(UIButton *)VerificationCodeButton
{
__block NSInteger timeout = [_CountdownTime integerValue];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_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);
dispatch_source_set_event_handler(_timer, ^{
if(timeout<=0){
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
[VerificationCodeButton setTitle:@"重新獲取" forState:UIControlStateNormal];
VerificationCodeButton.userInteractionEnabled = YES;
VerificationCodeButton.alpha = 1.0;
VerificationCodeButton.backgroundColor = [UIColor whiteColor];
});
} else {
NSString *strTime = [NSString stringWithFormat:@"%lds", (long)timeout];
dispatch_async(dispatch_get_main_queue(), ^{
[VerificationCodeButton setTitle:strTime forState:UIControlStateNormal];
VerificationCodeButton.userInteractionEnabled = NO;
VerificationCodeButton.backgroundColor = [UIColor lightTextColor];
});
timeout--;
}
});
dispatch_resume(_timer);
}
總結(jié)
以上所述是小編給大家介紹的iOS 驗證碼按鈕倒計時功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS開發(fā)中ViewController的頁面跳轉(zhuǎn)和彈出模態(tài)
這篇文章主要介紹了iOS開發(fā)中ViewController的頁面跳轉(zhuǎn)和彈出模態(tài),ViewController是MVC開發(fā)模式中一個重要的類,需要的朋友可以參考下2015-10-10
iOS利用Label實現(xiàn)的簡單高性能標簽TagView
這篇文章主要給大家介紹了關(guān)于iOS利用Label實現(xiàn)的簡單高性能標簽TagView的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-03-03
解決Alamofire庫在iOS7下設(shè)置Head無效的問題
本文主要介紹Alamofire庫在iOS下設(shè)置Head,這里通過代碼實例解決不同版本的IOS系統(tǒng)出現(xiàn)的問題,有需要的小伙伴可以參考下2016-07-07
iOS開發(fā)之攔截URL轉(zhuǎn)換成本地路由模塊URLRewrite詳解
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)之攔截URL轉(zhuǎn)換成本地路由模塊URLRewrite的相關(guān)資料,這是最近在工作中遇到的一個需求,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編來一起看看吧。2017-08-08

