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 ...
随机推荐
- 还能输入多少字?(JS动态计算)
<div class="m-form ovh"> <div class="hd"> <span class="fr&qu ...
- cuda(1) 最大并发量
Created on 2013-8-5URL : http://blog.sina.com.cn/s/blog_a502f1a30101mi6t.html@author: zhxfl转载请说明出处 c ...
- Microsoft Visual Studio 2013如何设置查找头文件的路径
- 普通身份运行Tomcat
普通身份运行Tomcat 转载1 权限分配问题 su - username -c “command”这样的形式可以使用任意一个有执行权限的用户执行 -c后边的命令. 注意,- username中间 ...
- Xilinx ISE14.7 安装教程(转)
文章来源http://blog.chinaaet.com/crazybird/p/39693 作者:crazybird **************************************** ...
- 【JAVA - SSM】之MyBatis与原生JDBC、Hibernate访问数据库的比较
首先来看一下原生JDBC访问数据库的代码: public static void main(String[] args) { // 数据库连接 Connection connection = null ...
- ctrl+z的JAVA实现,借助了命令模式(command pattern)
前些天学习<<JAVA与模式>>,到命令模式时,随带给了一个CTRL+Z案例的实现,想来学习编程这么久,CTRL+Z还没有认真实现过. 因此,借助JAVA与模式里面的源代码,自 ...
- MachineKey
我是在收到用户发来的这个错误信息的截图后才认识到什么是MachineKey的. 有关MachineKey的概念.MachineKey的生成以及web.config文件里的配置,网上一搜一大堆,为了方便 ...
- Cocos2d-js 3.0 alp2 使用指南
Download Cocos2d-JS: http://www.cocos2d-x.org/download Unzip and copy to C:/ Download JDK: http://ww ...
- RichTextBox选中文本时往自己的其他的位置实现拖拽
private void Form1_Load(object sender, EventArgs e) { richTextBox1.AllowDrop = true; richTextB ...