- (IBAction)buttonActon:(UIButton *)sender
{
sender.userInteractionEnabled = NO;
__block int count = 5;
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 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(_timer, ^{
if (count == 0) {
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
sender.userInteractionEnabled = YES;
sender.titleLabel.font = [UIFont systemFontOfSize:15];
sender.backgroundColor = [UIColor orangeColor];
[sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[sender setTitle:@"发送验证码" forState:UIControlStateNormal];
});
}
else
{
NSString *str = [NSString stringWithFormat:@"剩余(%d)s",count];
dispatch_async(dispatch_get_main_queue(), ^{
sender.backgroundColor = [UIColor grayColor];
[sender setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[sender setTitle:str forState:UIControlStateNormal];
});
count--;
}
});
dispatch_resume(_timer);
}
- (void)dealloc
{
if (_timer) dispatch_source_cancel(_timer);
}