驗證碼倒計時按鈕的運用長短常廣泛的,該Blog就和你一路來完成驗證碼倒計時的後果,界說一個發送驗證碼的按鈕,添加點擊事宜,詳細內容以下


詳細代碼:
界說一個發送驗證碼的按鈕,添加點擊事宜
//發送驗證碼按鈕 _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:@"發送驗證碼" forState:UIControlStateNormal]; [_sentCodeBtn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]]; //設置圓角 [_sentCodeBtn.layer setCornerRadius:3.0f]; [_sentCodeBtn.layer setShouldRasterize:YES]; [_sentCodeBtn.layer setRasterizationScale:[UIScreen mainScreen].scale]; //發送事宜 [_sentCodeBtn addTarget:self action:@selector(sentCodeMethod) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_sentCodeBtn];
監聽事宜:
//發送驗證碼
-(void)sentCodeMethod{
NSLog(@"發送驗證碼。。");
//計時器發送驗證碼
[self sentPhoneCodeTimeMethod];
//挪用發送驗證碼接口-》
}
//計時器發送驗證碼
-(void)sentPhoneCodeTimeMethod{
//倒計不時間 - 60秒
__block NSInteger timeOut = 59;
//履行隊列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//計時器 -》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);
//主線程設置按鈕款式-》
dispatch_async(dispatch_get_main_queue(), ^{
[_sentCodeBtn setTitle:@"發送驗證碼" forState:UIControlStateNormal];
[_sentCodeBtn setUserInteractionEnabled:YES];
});
}else{
//開端計時
//殘剩秒數 seconds
NSInteger seconds = timeOut % 60;
NSString *strTime = [NSString stringWithFormat:@"%.1ld",seconds];
//主線程設置按鈕款式
dispatch_async(dispatch_get_main_queue(), ^{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[_sentCodeBtn setTitle:[NSString stringWithFormat:@"%@S後從新發送",strTime] forState:UIControlStateNormal];
[UIView commitAnimations];
//計時器件不許可點擊
[_sentCodeBtn setUserInteractionEnabled:NO];
});
timeOut--;
}
});
dispatch_resume(timer);
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助。
【IOS完成驗證碼倒計時功效(二)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!