在iOS中,计时器是比较常用的,用于统计累加数据或者倒计时等,计时器大概有那么三种,分别是:
NSTimer
CADisplayLink
dispatch_source_t
NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
image.png
如果选择的mode是default的话,当滑动scrollView的时候,定时器是会停止的,你可以将mode设置为common。
特性:
使用场景:
从原理上可以看出,CADisplayLink适合做界面的不停重绘,比如视频播放的时候需要不停地获取下一帧用于界面渲染。
优点:
对比
dispatch_source_t是可以重复利用的,当我们在一个页面上,需要多次用到时钟的话,可以将dispatch_source_t保存为属性,避免提前释放,然后循环挂起和恢复,就可以达到多次利用的效果:
@property (nonatomic, strong) dispatch_source_t timer;
@property (nonatomic, assign) BOOL isSuspend; //定时器挂起状态
isSuspend记录下挂起的状态,因为dispatch_source_t的suspend和resume要依次进行,不然会crash,而且必须在resume的状态下,才能执行cancel,不然也会crash!!isSuspend默认为YES,因为首次需要resume以启动定时器!
- (dispatch_source_t)timer
{
if (!_timer) {
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
uint64_t interval = (uint64_t)(XYHeyMediaPhotoTimerInterval * NSEC_PER_SEC);
dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, interval, 0);
@weakify(self);
dispatch_source_set_event_handler(_timer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
[self updatePhotoProgress];
});
});
}
return _timer;
}
创建定时器,设置线程,启动时间,时间间隔,以及执行block,如果只执行一次,在block中调用cancel即可,我们这里默认为repeat!
- (void)resumeTimer
{
if (self.isSuspend) {
dispatch_resume(self.timer);
self.isSuspend = NO;
}
}
在需要启动时钟的时候调用上述方法resumeTimer,只有在已挂起的状态才能执行成功,同理,挂起操作:
- (void)suspendTimer
{
if (!self.isSuspend) {
dispatch_suspend(self.timer);
self.isSuspend = YES;
}
}
利用resumeTimer和suspendTimer,就可以重复利用该定时器了!!
当我页面销毁的时候,要主动将定时器销毁掉:
- (void)dealloc
{
if (_timer) {
if (_isSuspend) {
dispatch_resume(_timer);
}
dispatch_source_cancel(_timer);
_timer = nil;
}
}
__block int count= 60;
[self.sendCodeBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[self.sendCodeBtn setTitle:[NSString stringWithFormat:@"%d s",count] forState:UIControlStateNormal];
//定时器开始执行的延时时间
NSTimeInterval delayTime = 1.0f;
//定时器的间隔时间
NSTimeInterval timeInterval = 1.0f;
//创建子线程队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//使用之前创建的队列来创建计时器
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//设置延时的执行时间,delayTime为要延时的秒数
dispatch_time_t startDelayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime*NSEC_PER_SEC));
//设置计时器
dispatch_source_set_timer(self.timer, startDelayTime, timeInterval*NSEC_PER_SEC, 0.1*NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timer, ^{
//执行事件
/**
* 回主线程更新UI
*/
count--;
dispatch_async(dispatch_get_main_queue(), ^{
if (count==0) {
dispatch_cancel(self.timer);//取消定时器
self.timer = nil;
self.sendCodeBtn.enabled = YES;
[self.sendCodeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
[self.sendCodeBtn setTitleColor:[UIColor colorWithRed:254/255. green:218/255. blue:92/255. alpha:1] forState:UIControlStateNormal];
}
else{
self.sendCodeBtn.enabled = NO;
[self.sendCodeBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[self.sendCodeBtn setTitle:[NSString stringWithFormat:@"%d s",count] forState:UIControlStateNormal];
}
});
});
//启动计时器
dispatch_resume(_timer);