iOS 驗證碼按鈕倒計時功能
更新時間:2017年07月27日 09:09:30 作者:鴻鵠當(dāng)高遠(yuǎn)
在app注冊或者登錄需要驗證碼的地方、為了避免短時間內(nèi)刷驗證碼、往往會加上一層驗證當(dāng)?shù)褂嫊r結(jié)束后、可以重新獲取,關(guān)于ios 驗證碼按鈕倒計時功能大家可以參考下本文
在app 注冊或者登錄 需要驗證碼的地方、為了避免短時間內(nèi)刷驗證碼、往往會加上一層驗證。
倒計時結(jié)束后、可以重新獲??!
代碼實(shí)現(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ā)使用UITableView制作N級下拉菜單的示例
這篇文章主要介紹了iOS開發(fā)使用UITableView制作N級下拉菜單的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01iOS從App跳轉(zhuǎn)至系統(tǒng)設(shè)置菜單各功能項的編寫方法講解
這篇文章主要介紹了iOS從App跳轉(zhuǎn)至系統(tǒng)設(shè)置菜單各功能項的編寫方法講解,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04