免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3691 | 回复: 0
打印 上一主题 下一主题

[iOS] iOS验证码倒计时(GCD实现) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-28 11:21 |只看该作者 |倒序浏览
  1. + (void)verificationCode:(void(^)())blockYes blockNo:(void(^)(id time))blockNo {
  2.     __block int timeout=60; //倒计时时间
  3.     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  4.     dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  5.     dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  6.     dispatch_source_set_event_handler(_timer, ^{
  7.         if(timeout<=0){ //倒计时结束,关闭
  8.             dispatch_source_cancel(_timer);
  9.             dispatch_async(dispatch_get_main_queue(), ^{
  10.                 //设置界面的按钮显示 根据自己需求设置
  11.                 blockYes();
  12.             });
  13.         }else{
  14.             //            int minutes = timeout / 60;
  15.             int seconds = timeout % 60;
  16.             NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
  17.             dispatch_async(dispatch_get_main_queue(), ^{
  18.                 //设置界面的按钮显示 根据自己需求设置
  19.                 NSLog(@"____%@",strTime);
  20.                 blockNo(strTime);
  21.                
  22.             });
  23.             timeout--;
  24.         }
  25.     });
  26.     dispatch_resume(_timer);
  27. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP