Created
December 1, 2016 08:13
-
-
Save HarrisHan/8a3a257765f4bcd364a61280e3f9e9fd to your computer and use it in GitHub Desktop.
count down using button and NSTimer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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