GCDTimer
#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的更多相关文章
- swift - 封装 GCDTimer 和 NSTimer
封装的类代码 import UIKit /// 控制定时器的类 class ZDTimerTool: NSObject { /// 定时器 // private var timer: Timer? / ...
- Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用
OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...
- 我的runloop学习笔记
前言:公司项目终于忙的差不多了,最近比较闲,想起叶大说过的iOS面试三把刀,GCD.runtime.runloop,runtime之前已经总结过了,GCD在另一篇博客里也做了一些小总结,今天准备把ru ...
- CAGradientLayer的一些属性解析
CAGradientLayer的一些属性解析 iOS中Layer的坐标系统: 效果: - (void)viewDidLoad { [super viewDidLoad]; CAGradientLaye ...
- Swift - UITableViewCell倒计时重用解决方案
Swift - UITableViewCell倒计时重用解决方案 效果 源码 https://github.com/YouXianMing/Swift-Animations // // CountDo ...
- GCD编程 之 略微提高篇
额外任务:学习YouXianMing封装好的GCD源码 1.GCD串行队列与并发队列 串行队列一次只执行一个线程,按照添加到队列的顺序依次执行 并发队列一次可以执行多个线程,线程的执行没有先后 ...
- GCD的简单封装
扩展: dispatch_block_t :无参数block,使用起来很简单 下载链接:http://pan.baidu.com/s/1bndN6Yb ]; } //定时器 - (voi ...
- ios 中定时器:NSTimer, CADisplayLink, GCD
#import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...
- CAGradientLayer的一些属性解析-b
CAGradientLayer的一些属性解析 iOS中Layer的坐标系统: 效果: - (void)viewDidLoad { [super viewDidLoad]; CAGradientLaye ...
随机推荐
- .NET+Oracle 分页
http://www.cnblogs.com/Jusoc/archive/2011/08/28/2156530.html#commentform .在oracle的sqlplus或其他工具中运行一下p ...
- LR使用
1.协议的选择 Loadrunner的协议选择众多,想正确的选择脚本协议,首先要从Loadrunner的工作原理上深入的理解协议的作用和意义, Loadrunner启动后,在任务栏上有一个loadru ...
- 排序算法_HeapSort
大根堆排序的基本思想: 1) 先将初始文件R[1..n]建成一个大根堆,此堆为初始的无序区; 2) 再将关键字最大的记录R[1](即堆顶)和无序区的最后一个记录R[n]交换, 由此得到新的无序区 ...
- Git 使用相关
GIT命令和使用 安装git完成后,创建工程目录,右键运行bash here 1.查看git当前配置信息: git config -l 2.配置用户名.邮箱git config --global us ...
- Javascript and AJAX with Yii(在yii 中使用 javascript 和ajax)
英文原文:http://www.yiiframework.com/wiki/394/javascript-and-ajax-with-yii /*** http://www.yiiframework. ...
- group by子句的三点注意项
1.在含有统计函数的select语句中,如果不使用group by子句,那么select子句中只允许出现统计函数,其他任何字段都不允许出现: 2.在含有统计函数的select语句中,如果使用了grou ...
- asp.net mvc 提交model 接收不了
[HttpPost] //[ValidateInput(false)] public ActionResult AddNews1(_54Young_News_Model.m ...
- 关于PHP开发的9条建议
这篇文章主要介绍了关于PHP开发的9条建议,都是个人的一些经验总结,有需要的小伙伴可以参考下. 本文只是个人从实际开发经验中总结的一些东西,并不是什么名言警句,写出来有两个目的:一是时刻提醒自己要按照 ...
- VS2013/2012 下无法打开 源 文件“stdafx.h”的解决方法
VS2013/2012下代码一写上去保存就报错了,下方提示无法打开 源 文件“stdafx.h” 如图: 百度了一下,对于VS2010有这样的方法可以解决: 在项目属性中展开C/C++,选择常规,在附 ...
- hashCode与equals详解
在工作中写业务类通常都会重写hashCode与equals方法,而这两个方法的区别与用途也常常被问道.平时也只是大概知道这二者的用途,今天闲下来,查阅资料加上自己的理 解,总结记录下. hashCod ...