倒计时实现由三种方式,一种是NSTimer,第二种是是CADisplayLink,第三种是通过GCD的方式来实现,效果图如下:
倒计时.gif
NSTimer
NSTimer作为倒计时有两个重要的执行方式:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
倒计时代码:
- (void)setupTimer {
self.topLabel.text = [NSString stringWithFormat:@"%ld",topCount];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTopLabel) userInfo:nil repeats:YES];
}
- (void)updateTopLabel {
topCount -= 1;
self.topLabel.text = [NSString stringW