#import <Foundation/Foundation.h>

 @interface JKTimerManager : NSObject

 + (instancetype)sharedTimerManager;

 /**
* 启动一个timer,默认精度为0.1秒
*
* @param name timer的名称,作为唯一标识
* @param timerInterval 执行的时间间隔
* @param queue timer将被放入的队列,也就是最终action执行的队列。传入nil将自动放到一个子线程队列中
* @param repeats timer是否循环调用
* @param action 时间间隔到点时执行的block
*/
- (void)scheduledDispatchTimerWithName:(NSString *)name timeInterval:(NSTimeInterval)timerInterval queue:(dispatch_queue_t)queue repeats:(BOOL)repeats action:(dispatch_block_t)action; /**
* 撤销某个timer
*
* @param name timer的名称,唯一标识
*/
- (void)cancelTimerWithName:(NSString *)name; /**
* 撤销所有timer
*/
- (void)cancelAllTimer; @end
 #import "JKTimerManager.h"

 @interface JKTimerManager ()

 @property (nonatomic,strong) NSMutableDictionary *timerContainer;

 @end

 @implementation JKTimerManager

 + (instancetype)sharedTimerManager {
static JKTimerManager *manager = nil;
static dispatch_once_t token;
dispatch_once(&token, ^{
manager = [[JKTimerManager alloc] init];
}); return manager;
} - (NSMutableDictionary *)timerContainer {
if (!_timerContainer) {
_timerContainer = [NSMutableDictionary dictionary];
}
return _timerContainer;
} - (void)scheduledDispatchTimerWithName:(NSString *)name
timeInterval:(NSTimeInterval)timerInterval
queue:(dispatch_queue_t)queue
repeats:(BOOL)repeats
action:(dispatch_block_t)action {
if (name == nil)
return;
if (queue == nil)
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); dispatch_source_t timer = [self.timerContainer objectForKey:name];
if (!timer) {
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, , , queue);
dispatch_resume(timer);
[self.timerContainer setObject:timer forKey:name];
} dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, timerInterval * NSEC_PER_SEC), timerInterval * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
__weak typeof(self) weakSelf = self;
dispatch_source_set_event_handler(timer, ^{
if (action) {
action();
if (!repeats) {
[weakSelf cancelTimerWithName:name];
}
}
}); } - (void)cancelTimerWithName:(NSString *)name {
dispatch_source_t timer = [self.timerContainer objectForKey:name];
if (!timer) {
return;
} [self.timerContainer removeObjectForKey:name];
dispatch_source_cancel(timer);
} - (void)cancelAllTimer {
for (NSString *name in self.timerContainer.allKeys) {
[self cancelTimerWithName:name];
}
} @end

GCDTimer的更多相关文章

  1. swift - 封装 GCDTimer 和 NSTimer

    封装的类代码 import UIKit /// 控制定时器的类 class ZDTimerTool: NSObject { /// 定时器 // private var timer: Timer? / ...

  2. Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用

    OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...

  3. 我的runloop学习笔记

    前言:公司项目终于忙的差不多了,最近比较闲,想起叶大说过的iOS面试三把刀,GCD.runtime.runloop,runtime之前已经总结过了,GCD在另一篇博客里也做了一些小总结,今天准备把ru ...

  4. CAGradientLayer的一些属性解析

    CAGradientLayer的一些属性解析 iOS中Layer的坐标系统: 效果: - (void)viewDidLoad { [super viewDidLoad]; CAGradientLaye ...

  5. Swift - UITableViewCell倒计时重用解决方案

    Swift - UITableViewCell倒计时重用解决方案 效果 源码 https://github.com/YouXianMing/Swift-Animations // // CountDo ...

  6. GCD编程 之 略微提高篇

    额外任务:学习YouXianMing封装好的GCD源码   1.GCD串行队列与并发队列   串行队列一次只执行一个线程,按照添加到队列的顺序依次执行 并发队列一次可以执行多个线程,线程的执行没有先后 ...

  7. GCD的简单封装

    扩展: dispatch_block_t :无参数block,使用起来很简单 下载链接:http://pan.baidu.com/s/1bndN6Yb    ]; }     //定时器 - (voi ...

  8. ios 中定时器:NSTimer, CADisplayLink, GCD

    #import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...

  9. CAGradientLayer的一些属性解析-b

    CAGradientLayer的一些属性解析 iOS中Layer的坐标系统: 效果: - (void)viewDidLoad { [super viewDidLoad]; CAGradientLaye ...

随机推荐

  1. sql getdate() 时间格式设置

      Sql Server 中一个非常强大的日期格式化函数常用: Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16 Select CONVE ...

  2. An exception occurred during configuration of persistence layer.

    配置文件放在bin文件夹下(注意:hibernate.cfg.xml文件名不要随便改动)

  3. HTML5 Canvas核心技术—图形、动画与游戏开发.pdf6

    操作图像的像素:getImageData() putImageData() ImageData对象 调用getImageData()方法实际是获取了一个指向ImageData对象的引用,返回的对象包含 ...

  4. WE-1202 (JGX-X5 v1.3)刷openwrt

    当前固件版本显示 固件版本 RippleOS硬件型号 RippleTek WE-1202CPU型号是 MT7620N路由主板上面丝印是 JGX-X5  v1.3 买的时候选择的RippleOS固件,后 ...

  5. 获得URl信息

      public class GetUrlInfo {     public static void printfInfo(URL url)throws Exception     {         ...

  6. php非阻塞执行系统命令

    大家都知道php调用系统命令常用的主要有以下几种方法: 如exec(), system(), passthru(), shell_exec() 这几个函数的用法在此不做说明,有需要的请查阅php相关手 ...

  7. 动态生成修改aspx文件

    using System.IO; using System.Text;/// <summary>/// SCPage 的摘要说明 修改 aspx 和aspx.cs文件/// </su ...

  8. 【python自动化第六篇:面向对象】

    知识点概览: 面向对象的介绍 面向对象的特性(class,object)实例变量,类变量 面型对象编程的介绍 其他概念 一.面向对象介绍 编程范式:面向对象,面向过程,函数式编程 面向过程:通过一组指 ...

  9. Codeigniter数据库操作

    //查询: $query = $this->db_query("SELECT * FROM table"); ================================ ...

  10. Eclipse中修改SVN用户名和密码方法[转]

    由于在svn 的界面中并没有为我们提供直接更换用户名密码的地方,所以一旦我们需要更换用户名的就需要自己想一些办法. 解决方案: 在Eclipse 使用SVN 的过程中大多数人往往习惯把访问SVN 的用 ...