IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時功能(二)
更新時間:2016年04月13日 15:26:41 作者:學(xué)偉愛OS
這篇文章主要介紹了IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時功能,點(diǎn)擊獲取驗(yàn)證碼,進(jìn)入時間倒計(jì)時,感興趣的小伙伴們可以參考一下
驗(yàn)證碼倒計(jì)時按鈕的應(yīng)用是非常普遍的,該Blog就和你一起來實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時的效果,定義一個發(fā)送驗(yàn)證碼的按鈕,添加點(diǎn)擊事件,具體內(nèi)容如下


具體代碼:
定義一個發(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)聽事件:
//發(fā)送驗(yàn)證碼
-(void)sentCodeMethod{
NSLog(@"發(fā)送驗(yàn)證碼。。");
//計(jì)時器發(fā)送驗(yàn)證碼
[self sentPhoneCodeTimeMethod];
//調(diào)用發(fā)送驗(yàn)證碼接口-》
}
//計(jì)時器發(fā)送驗(yàn)證碼
-(void)sentPhoneCodeTimeMethod{
//倒計(jì)時時間 - 60秒
__block NSInteger timeOut = 59;
//執(zhí)行隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//計(jì)時器 -》dispatch_source_set_timer自動生成
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{
//開始計(jì)時
//剩余秒數(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ì)時器件不允許點(diǎn)擊
[_sentCodeBtn setUserInteractionEnabled:NO];
});
timeOut--;
}
});
dispatch_resume(timer);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
您可能感興趣的文章:
- IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時功能(一)
- iOS獲取短信驗(yàn)證碼倒計(jì)時的兩種實(shí)現(xiàn)方法
- Swift實(shí)現(xiàn)iOS應(yīng)用中短信驗(yàn)證碼倒計(jì)時功能的實(shí)例分享
- iOS 驗(yàn)證碼按鈕倒計(jì)時功能
- iOS實(shí)現(xiàn)手機(jī)獲取驗(yàn)證碼倒計(jì)時效果
- iOS獲取驗(yàn)證碼倒計(jì)時效果
- iOS發(fā)送驗(yàn)證碼倒計(jì)時應(yīng)用
- iOS實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時
- iOS登錄時驗(yàn)證手機(jī)號與倒計(jì)時發(fā)送驗(yàn)證碼問題詳解
相關(guān)文章
基于iOS Realm數(shù)據(jù)庫的使用實(shí)例詳解
下面小編就為大家分享一篇基于iOS Realm數(shù)據(jù)庫的使用實(shí)例詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
關(guān)于iOS屏幕旋轉(zhuǎn)的一些注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于iOS屏幕旋轉(zhuǎn)的一些注意事項(xiàng),文中通過一步步的步驟介紹的很詳細(xì),相信對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,有需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-01-01
Objective-C const常量的優(yōu)雅使用方法
這篇文章主要為大家介紹了Objective-C const常量的優(yōu)雅使用方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

