Skip to content

Instantly share code, notes, and snippets.

@HarrisHan
Created December 1, 2016 08:13
Show Gist options
  • Save HarrisHan/a4dbc4926e7365fe2588ec507aa50234 to your computer and use it in GitHub Desktop.
Save HarrisHan/a4dbc4926e7365fe2588ec507aa50234 to your computer and use it in GitHub Desktop.
count down using button and NSTimer
- (void)startTimer
{
self.leftSecondsNum = 59;
self.fetchCodeBtn.enabled = NO;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshCountNum) userInfo:nil repeats:YES];
[self.fetchCodeBtn setTitle:[NSString stringWithFormat:@"重新获取(%ld)", (long)self.leftSecondsNum] forState:UIControlStateDisabled];
}
- (void)endupTimer
{
[self.timer invalidate];
self.timer = nil;
self.fetchCodeBtn.enabled = YES;
}
- (void)refreshCountNum
{
self.leftSecondsNum -= 1;
if (self.leftSecondsNum < 0) {
[self endupTimer];
return;
}
[self.fetchCodeBtn setTitle:[NSString stringWithFormat:@"重新获取(%ld)", (long)self.leftSecondsNum] forState:UIControlStateDisabled];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment