GCDTimer

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface JKTimerManager : NSObject
 4 
 5 + (instancetype)sharedTimerManager;
 6 
 7 /**
 8  *  启动一个timer,默认精度为0.1秒
 9  *
10  *  @param name          timer的名称,作为唯一标识
11  *  @param timerInterval 执行的时间间隔
12  *  @param queue         timer将被放入的队列,也就是最终action执行的队列。传入nil将自动放到一个子线程队列中
13  *  @param repeats       timer是否循环调用
14  *  @param action        时间间隔到点时执行的block
15  */
16 - (void)scheduledDispatchTimerWithName:(NSString *)name timeInterval:(NSTimeInterval)timerInterval queue:(dispatch_queue_t)queue repeats:(BOOL)repeats action:(dispatch_block_t)action;
17 
18 /**
19  *  撤销某个timer
20  *
21  *  @param name timer的名称,唯一标识
22  */
23 - (void)cancelTimerWithName:(NSString *)name;
24 
25 /**
26  *  撤销所有timer 
27  */
28 - (void)cancelAllTimer;
29 
30 @end

 

 1 #import "JKTimerManager.h"
 2 
 3 @interface JKTimerManager ()
 4 
 5 @property (nonatomic,strong) NSMutableDictionary *timerContainer;
 6 
 7 @end
 8 
 9 @implementation JKTimerManager
10 
11 + (instancetype)sharedTimerManager {
12     static JKTimerManager *manager = nil;
13     static dispatch_once_t token;
14     dispatch_once(&token, ^{
15         manager = [[JKTimerManager alloc] init];
16     });
17     
18     return manager;
19 }
20 
21 - (NSMutableDictionary *)timerContainer {
22     if (!_timerContainer) {
23         _timerContainer = [NSMutableDictionary dictionary];
24     }
25     return _timerContainer;
26 }
27 
28 - (void)scheduledDispatchTimerWithName:(NSString *)name
29                           timeInterval:(NSTimeInterval)timerInterval
30                                  queue:(dispatch_queue_t)queue
31                                repeats:(BOOL)repeats
32                                 action:(dispatch_block_t)action {
33     if (name == nil)
34         return;
35     if (queue == nil)
36         queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
37     
38     dispatch_source_t timer = [self.timerContainer objectForKey:name];
39     if (!timer) {
40         timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
41         dispatch_resume(timer);
42         [self.timerContainer setObject:timer forKey:name];
43     }
44     
45     dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, timerInterval * NSEC_PER_SEC), timerInterval * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
46     __weak typeof(self) weakSelf = self;
47     dispatch_source_set_event_handler(timer, ^{
48         if (action) {
49             action();
50             if (!repeats) {
51                 [weakSelf cancelTimerWithName:name];
52             }
53         }
54     });
55     
56 }
57 
58 - (void)cancelTimerWithName:(NSString *)name {
59     dispatch_source_t timer = [self.timerContainer objectForKey:name];
60     if (!timer) {
61         return;
62     }
63     
64     [self.timerContainer removeObjectForKey:name];
65     dispatch_source_cancel(timer);
66 }
67 
68 - (void)cancelAllTimer {
69     for (NSString *name in self.timerContainer.allKeys) {
70         [self cancelTimerWithName:name];
71     }
72 }
73 
74 @end

 

posted @ 2016-08-02 17:08  Emerys  阅读(361)  评论(0编辑  收藏  举报