在开发过程中,对于定时器相信大家都不陌生,在//www.greatytc.com/p/0194a0866872 这篇文章中,我有谈过各种定时器的优缺点,这里就简单封装一个实用的不依赖于RunLoop运行模式的GCD定时器
-
首先GCD定时器本质也是对象必须有一个强引用应用着,不然就会被释放,由于提供的是类方法,就定义一个全局静态变量字典来保存timer对象
//存放timer对象
static NSMutableDictionary *timerDictionary;
//根据index来存储不同的timer对象
static NSInteger timerIndex = 0;
- 提供了一下两个类方法来初始化定时器,区别在于第一个初始化完成后自动开启定时器,第二个需要自己选择开启与否
//初始化定时器,默认初始化完成后开启
- (void)timerWithTimeInterval:(NSTimeInterval)timeInterval dispatchQueue:(dispatch_queue_t)dispatchQueue withBlock:(void(^)(XHR_dispatch_timer *))block;
//初始化定时器,需要手动开启
- (void)timerWithTimeInterval:(NSTimeInterval)timeInterval dispatchQueue:(dispatch_queue_t)dispatchQueue isOn:(BOOL)isOn withBlock:(void(^)(XHR_dispatch_timer *))block;
- 具体方法的实现如下
//初始化定时器
- (void)timerWithTimeInterval:(NSTimeInterval)timeInterval dispatchQueue:(dispatch_queue_t)dispatchQueue withBlock:(void (^)())block
{
//默认方法自动开启定时器
[self timerWithTimeInterval:timeInterval dispatchQueue:dispatchQueue isOn:YES withBlock:block];
}
-
(void)timerWithTimeInterval:(NSTimeInterval)timeInterval dispatchQueue:(dispatch_queue_t)dispatchQueue isOn:(BOOL)isOn withBlock:(void (^)(XHR_dispatch_timer *))block
{
XHR_dispatch_timer *timer = [[XHR_dispatch_timer alloc]initWithTimeInterval:timeInterval dispatchQueue:dispatchQueue withBlock:block];[self.timerDictionary setObject:timer forKey:[NSString stringWithFormat:@"timerIndex_%ld",timerIndex++]];
//根据设置是否需要在初始化完成后开启
if (isOn) {
timer.timerStatus = XHR_dispatch_timerStatusRuning;
}
}
- 那么解释一下这里的XHR_dispatch_timer,它是定义在XHRTimer这个单例内的一个私有类,是用来管理每一个timer对象的,以便在创建不同的timer对象执行不同的任务,并且定义了一个定时器状态枚举来标记运作状态,通过调整运作状态就可以控制定时器的开启、暂停、取消等操作,也避免了多次开启、暂停、取消而引起的bug,具体实现如下
- (instancetype)initWithTimeInterval:(NSTimeInterval)timeInterval dispatchQueue:(dispatch_queue_t)dispatchQueue withBlock:(void(^)(XHR_dispatch_timer *))block
{
if (self = [super init]) {
//timer本质也是对象必须有强应用引用,不然就会被释放
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatchQueue);
//设置开始时间和时间间隔
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, timeInterval * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
//设置回调时间
dispatch_source_set_event_handler(self.timer, ^{
block(self);
});
self.timerStatus = XHR_dispatch_timerStatusSuspend;
}
return self;
} - (void)setTimerStatus:(XHR_dispatch_timerStatus)timerStatus
{
if (!self.timer) return;
if (timerStatus == XHR_dispatch_timerStatusSuspend && _timerStatus != XHR_dispatch_timerStatusSuspend) {
dispatch_suspend(self.timer);
}
else if (timerStatus == XHR_dispatch_timerStatusRuning && _timerStatus == XHR_dispatch_timerStatusSuspend)
{
dispatch_resume(self.timer);
}
else if(timerStatus == XHR_dispatch_timerStatusCanceled && _timerStatus != XHR_dispatch_timerStatusCanceled)
{
dispatch_cancel(self.timer);
self.timer = nil;
}
_timerStatus = timerStatus;
}
- 暂停定时器的方法
//暂停定时器
- (void)suspend:(XHR_dispatch_timer *)timer
{
if (timer) {
timer.timerStatus = XHR_dispatch_timerStatusSuspend;
}
} - (void)suspendAllTimer
{
[self.timerDictionary enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, XHR_dispatch_timer *_Nonnull obj, BOOL * _Nonnull stop) {
obj.timerStatus = XHR_dispatch_timerStatusSuspend;
}];
}
- 手动开启/继续 定时器的方法
//手动开启/继续 定时器
- (void)resume:(XHR_dispatch_timer *)timer
{
if (timer) {
timer.timerStatus = XHR_dispatch_timerStatusRuning;
}
} - (void)resumeAllTimer
{
[self.timerDictionary enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, XHR_dispatch_timer *_Nonnull obj, BOOL * _Nonnull stop) {
obj.timerStatus = XHR_dispatch_timerStatusRuning;
}];
}
- 取消定时器
//取消定时器
//取消定时器
- (void)cancel:(XHR_dispatch_timer *)timer
{
if (timer) {
timer.timerStatus = XHR_dispatch_timerStatusCanceled;
}
[self.timerDictionary enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj isEqual:timer]) {
[self.timerDictionary removeObjectForKey:key];
return;
}
}];
} - (void)cancelAllTimer
{
[self.timerDictionary enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, XHR_dispatch_timer *_Nonnull obj, BOOL * _Nonnull stop) {
obj.timerStatus = XHR_dispatch_timerStatusCanceled;
}];
[self.timerDictionary removeAllObjects];
}
注:
- 调用方法和NSTimer类似,使用类方法,需要执行的代码直接写在方法的block中,非常方便.需要注意的是,如果取消了定时器,如果想重新开启就需要重新初始化,不然就会崩溃。
- bolock传出去的参数是为了供外部解决循环应用问题的
作者:胥鸿儒
demo地址:https://github.com/xuhongru/GCDTimer