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 ...
随机推荐
- HDU-2521 反素数
反素数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 《C语言程序设计现代方法》第2章 C语言基本概念
C语言的基本概念 第一个C程序例子. /* pun.c */ #include <stdio.h> int main(void) { printf("To C, or not t ...
- DevExpress的JavaScript脚本智能提示
http://www.cnblogs.com/zhaozhan/archive/2011/06/08/2075767.html ASPxScriptIntelliSense.js在安装目录下的Comp ...
- 项目杂记(MONTHS_BETWEEN,Having ,Spool)
1,oracle中计算年龄: select FLOOR(MONTHS_BETWEEN(SYSDATE, to_date('20130728', 'yyyymmdd')) / 12), trunc(mo ...
- [LeetCode] 86. Partition List 解题思路
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- useradd、passwd、userdel
useradd是新建用户 userdel -r 是删除用户 passwd是修改密码 groupadd是新建组 groupdel是删除组 useradd yonghu 为添加用户 echo " ...
- pg_dump实例详解(备份postgresql和greenplum数据库)
一.pg_dump的用法:数据库的导入导出是最常用的功能之一,每种数据库都提供有这方面的工具,例如Oracle的exp/imp,Informix的dbexp/dbimp,MySQL的mysqldump ...
- OGG问题 ORA-01403的处理办法
认识logdump分析工具及常用命令:http://book.51cto.com/art/201202/319253.htm http://www.killdb.com/2012/09/01/gold ...
- PAT 1069. The Black Hole of Numbers (20)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- linux下无法删除文件的原因
不废话,直接上命令操作.记录备案以后方便查阅 [root@xxxxxxx .ssh]# rm -rf authorized_keys2 rm: 无法删除"authorized_keys2&q ...