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 ...
随机推荐
- web前端相关的书籍
一.Javascript方面的书籍: 1 JavaScript权威指南(第6版):号称javascript圣经,前端必备:前端程序员学习核心JavaScript语言和由Web浏览器定义的JavaScr ...
- dos攻击与防御
SYN Flood攻击 标准的TCP三次握手过程如下: 客户端发送一个包含SYN标志的TCP报文,SYN即同步(Synchronize),同步报文会指明客户端使用的端口以及TCP连接的初始序号: 服 ...
- 门面模式 到 socket
http://www.cnblogs.com/java-my-life/archive/2012/05/02/2478101.html 1.门面模式定义: 门面模式是对象的结构模式,外部与一个子系统的 ...
- 主流智能手机屏幕材质介绍 及 LCD闪屏现象分析
TN TN(Twisted Nematic) 即扭曲向列型面板,属于有源矩阵液晶显示器中的一种.由于TFT是主动式矩阵LCD可让液晶的排列方式具有记忆性,不会在电流消失后马上恢复 ...
- date 、cal、bc
date cal date是显示日期与时间 date +%Y/%M/%D date +%Y/%m/%d date +%H:%M cal cal 2016
- Datediff函数 助你实现不同进制时间之间的运算
在VB开发环境中实现时间之间的加减运算有很多种方法,前不久自己无意中发现了Datediff函数,它能够比较简单.全面地实现我们比较常用的时间之间的运算,今由自己的研究,搞清了它的一些用法,拿来和大家分 ...
- 【iOS 7】使用UIScreenEdgePanGestureRecognizer实现swipe to pop效果
在iOS 7还没有发布的时候,各种App实现各种的swipe to pop效果,比如这里有一份简单的demo. 在iOS 7上,只要是符合系统的导航结构: - (BOOL)application:(U ...
- 半斤八两(创业兴家版 打工仔心声'98 Remix)
创业兴家打工仔 刻苦工作热诚日夜维系天天专心向上依足正轨 结力好比兄弟努力一生打工仔 相亲相爱朋情日夜传递彼此一家那用分高与低 要互相多鼓励 半斤八两 莫记往昔的创伤半斤八两 面对春光应插秧半斤八两 ...
- 右下角显示提示窗口(New-Object,COM)
$title=“title”$message="hello,world"有时候,要使用的实例的类保存在独立的库文件中,PowerShell默认未加载,就需要先加载库文件,然后再创建 ...
- 硝烟中的Scrum和XP-我们如何实施Scrum 15)多团队 Part 2/2 16)地理分散 17)检查列表 18)其他
引入"团队领导"角色 假设有3个团队开发同一个产品 红色的P是PO, 黑色的S是SM, 蓝色是其他团队成员; 如何决定哪些人属于哪个团队? 怎么分配成员? 有人觉得让PO来做人员分 ...