IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(二)
驗(yàn)證碼倒計(jì)時(shí)按鈕的應(yīng)用是非常普遍的,該Blog就和你一起來(lái)實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)的效果,定義一個(gè)發(fā)送驗(yàn)證碼的按鈕,添加點(diǎn)擊事件,具體內(nèi)容如下
具體代碼:
定義一個(gè)發(fā)送驗(yàn)證碼的按鈕,添加點(diǎn)擊事件
//發(fā)送驗(yàn)證碼按鈕 _sentCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 27 - 4 - 94, CGRectGetMinY(_registerCodeFD.frame) + 4, 94, 40)]; [_sentCodeBtn setBackgroundColor:colorWithRGBA(0, 191, 191, 0.9)]; [_sentCodeBtn setTitle:@"發(fā)送驗(yàn)證碼" forState:UIControlStateNormal]; [_sentCodeBtn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]]; //設(shè)置圓角 [_sentCodeBtn.layer setCornerRadius:3.0f]; [_sentCodeBtn.layer setShouldRasterize:YES]; [_sentCodeBtn.layer setRasterizationScale:[UIScreen mainScreen].scale]; //發(fā)送事件 [_sentCodeBtn addTarget:self action:@selector(sentCodeMethod) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_sentCodeBtn];
監(jiān)聽(tīng)事件:
//發(fā)送驗(yàn)證碼 -(void)sentCodeMethod{ NSLog(@"發(fā)送驗(yàn)證碼。。"); //計(jì)時(shí)器發(fā)送驗(yàn)證碼 [self sentPhoneCodeTimeMethod]; //調(diào)用發(fā)送驗(yàn)證碼接口-》 } //計(jì)時(shí)器發(fā)送驗(yàn)證碼 -(void)sentPhoneCodeTimeMethod{ //倒計(jì)時(shí)時(shí)間 - 60秒 __block NSInteger timeOut = 59; //執(zhí)行隊(duì)列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //計(jì)時(shí)器 -》dispatch_source_set_timer自動(dòng)生成 dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC); dispatch_source_set_event_handler(timer, ^{ if (timeOut <= 0) { dispatch_source_cancel(timer); //主線程設(shè)置按鈕樣式-》 dispatch_async(dispatch_get_main_queue(), ^{ [_sentCodeBtn setTitle:@"發(fā)送驗(yàn)證碼" forState:UIControlStateNormal]; [_sentCodeBtn setUserInteractionEnabled:YES]; }); }else{ //開(kāi)始計(jì)時(shí) //剩余秒數(shù) seconds NSInteger seconds = timeOut % 60; NSString *strTime = [NSString stringWithFormat:@"%.1ld",seconds]; //主線程設(shè)置按鈕樣式 dispatch_async(dispatch_get_main_queue(), ^{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0]; [_sentCodeBtn setTitle:[NSString stringWithFormat:@"%@S后重新發(fā)送",strTime] forState:UIControlStateNormal]; [UIView commitAnimations]; //計(jì)時(shí)器件不允許點(diǎn)擊 [_sentCodeBtn setUserInteractionEnabled:NO]; }); timeOut--; } }); dispatch_resume(timer); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(一)
- iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法
- Swift實(shí)現(xiàn)iOS應(yīng)用中短信驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)例分享
- iOS 驗(yàn)證碼按鈕倒計(jì)時(shí)功能
- iOS實(shí)現(xiàn)手機(jī)獲取驗(yàn)證碼倒計(jì)時(shí)效果
- iOS獲取驗(yàn)證碼倒計(jì)時(shí)效果
- iOS發(fā)送驗(yàn)證碼倒計(jì)時(shí)應(yīng)用
- iOS實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)
- iOS登錄時(shí)驗(yàn)證手機(jī)號(hào)與倒計(jì)時(shí)發(fā)送驗(yàn)證碼問(wèn)題詳解
相關(guān)文章
iOS開(kāi)發(fā)微信收款到賬語(yǔ)音提醒功能思路詳解
這篇文章主要介紹了iOS開(kāi)發(fā)微信收款到賬語(yǔ)音提醒功能思路詳解,需要的朋友可以參考下2017-09-09基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解
下面小編就為大家分享一篇基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01關(guān)于iOS屏幕旋轉(zhuǎn)的一些注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于iOS屏幕旋轉(zhuǎn)的一些注意事項(xiàng),文中通過(guò)一步步的步驟介紹的很詳細(xì),相信對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,有需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。2017-01-01iOS開(kāi)發(fā)之隱藏導(dǎo)航欄線的簡(jiǎn)單代碼
這篇文章主要介紹了iOS開(kāi)發(fā)之隱藏導(dǎo)航欄線的簡(jiǎn)單代碼,需要的朋友可以參考下2017-10-10Objective-C const常量的優(yōu)雅使用方法
這篇文章主要為大家介紹了Objective-C const常量的優(yōu)雅使用方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08ios 實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式
這篇文章主要介紹了ios實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式,第一種方式使用NSTimer來(lái)實(shí)現(xiàn),第二種方式使用GCD來(lái)實(shí)現(xiàn)。具體內(nèi)容詳情大家參考下本文2017-01-01