标签:
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 }
标签:
原文地址:http://www.cnblogs.com/qinxiaoguang/p/5443216.html