码迷,mamicode.com
首页 > 其他好文 > 详细

GCD实现验证码倒计时

时间:2016-04-28 17:04:02      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3    [_timeButton addTarget:self action:@selector(startTime) forControlEvents:UIControlEventTouchUpInside];
 4 }
 5 -(void)startTime{
 6     __block int timeout=30; //倒计时时间
 7     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 8     dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
 9     dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
10     dispatch_source_set_event_handler(_timer, ^{
11         if(timeout<=0){ //倒计时结束,关闭
12             dispatch_source_cancel(_timer);
13             dispatch_async(dispatch_get_main_queue(), ^{
14                 //设置界面的按钮显示 根据自己需求设置
15                 [_timeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
16                 _timeButton.userInteractionEnabled = YES;
17             });
18         }else{
19             int seconds = timeout % 60;
20             NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
21             dispatch_async(dispatch_get_main_queue(), ^{
22                 //设置界面的按钮显示 根据自己需求设置
23                 //NSLog(@"____%@",strTime);
24                 [UIView beginAnimations:nil context:nil];
25                 [UIView setAnimationDuration:1];
26                 [_timeButton setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
27                 [UIView commitAnimations];
28                 _timeButton.userInteractionEnabled = NO;
29             });
30             timeout--;
31         }
32     });
33     dispatch_resume(_timer);
34 }

 

GCD实现验证码倒计时

标签:

原文地址:http://www.cnblogs.com/qinxiaoguang/p/5443216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!