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ā)之為視圖繪制單(多)個(gè)圓角實(shí)例代碼
這篇文章主要介紹了IOS開(kāi)發(fā)之為視圖繪制單(多)個(gè)圓角實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
iOS開(kāi)發(fā)筆記--詳解UILabel的相關(guān)屬性設(shè)置
這篇文章主要介紹了iOS開(kāi)發(fā)筆記--詳解UILabel的相關(guān)屬性設(shè)置,對(duì)初學(xué)者具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11
iOS移動(dòng)端軟鍵盤彈起空白和滾動(dòng)穿透問(wèn)題解決方案
這篇文章主要為大家介紹了iOS移動(dòng)端軟鍵盤彈起空白和滾動(dòng)穿透問(wèn)題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
iOS仿微信搖一搖動(dòng)畫效果加震動(dòng)音效實(shí)例
這篇文章主要介紹了iOS仿微信搖一搖動(dòng)畫效果加震動(dòng)音效實(shí)例,詳細(xì)介紹了微信搖一搖功能的實(shí)現(xiàn)原理,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03
詳解iOS開(kāi)發(fā)中解析JSON中的boolean類型的數(shù)據(jù)遇到的問(wèn)題
這篇文章主要介紹了詳解iOS開(kāi)發(fā)中解析JSON中的boolean類型的數(shù)據(jù)遇到的問(wèn)題,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
MAUI模仿iOS多任務(wù)切換卡片滑動(dòng)的交互實(shí)現(xiàn)代碼
這篇文章主要介紹了[MAUI]模仿iOS多任務(wù)切換卡片滑動(dòng)的交互實(shí)現(xiàn),使用.NET MAU實(shí)現(xiàn)跨平臺(tái)支持,本項(xiàng)目可運(yùn)行于Android、iOS平臺(tái),需要的朋友可以參考下2023-05-05
iOS使用UICollectionView實(shí)現(xiàn)橫向滾動(dòng)照片效果
這篇文章主要為大家詳細(xì)介紹了iOS使用UICollectionView實(shí)現(xiàn)橫向滾動(dòng)照片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05

