Created
December 1, 2016 08:12
-
-
Save HarrisHan/959da6de8fb05f72d5ec4e29696d9ea2 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