iOS 验证码按钮倒计时功能

时间:2022-10-05 23:32:23

在app 注册或者登录 需要验证码的地方、为了避免短时间内刷验证码、往往会加上一层验证。

iOS 验证码按钮倒计时功能

倒计时结束后、可以重新获取!

iOS 验证码按钮倒计时功能

代码实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// _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);
}

总结

以上所述是小编给大家介绍的ios 验证码按钮倒计时功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/jukaiit/archive/2017/07/26/7238829.html