zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Cleve Moler MATLAB 创始人金秋10月中国大学校园行
    [原]ASP.NET中使用JQUERYEASYUI后,解决ClientScript.RegisterStartupScript 所注册脚本执行两次
    [原]ASP.NET中使用后端代码注册脚本 生成JQUERYEASYUI 的界面错位
    [原]jqueryeasyui 关闭tab如何自动切换到前一个tab
    [原创]C# 实例Oracle 备份,带进度提示
    停止Oracle 服务开机自动重启
    最新县及县以上行政区划代码(截止2009年12月31日)
    单元测试学习:无返回值,触发委托
    [笔记]GetRequestStream()超时问题(出现假死,卡住)
    asp.net 页面 css中图片不存在引发的异常
  • 原文地址:https://www.cnblogs.com/buakaw/p/5730000.html
Copyright © 2011-2022 走看看