-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGCDTimer.m
82 lines (73 loc) · 1.91 KB
/
GCDTimer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// GCDTimer.m
// Face_meal
//
// Created by funny on 2017/12/20.
// Copyright © 2017年 fu. All rights reserved.
//
#import "GCDTimer.h"
@interface GCDTimer ()
/**
定时器
*/
@property (nonatomic)dispatch_source_t timer;
@end
@implementation GCDTimer
/**
init
@param sec 延迟多久执行,单位为s
@param padding_sec 计时间隔,单位为s
@param task 执行的任务
@return
*/
- (instancetype)initWithStartAfter:(CGFloat)sec
padding:(CGFloat)padding_sec
task:(task)task{
if (self = [super init]) {
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0,
0,
queue);
//开始时间
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW,
sec * NSEC_PER_SEC);
//间隔时间
uint64_t interval = padding_sec * NSEC_PER_SEC;
dispatch_source_set_timer(self.timer,
start,
interval,
0);
//设置回调
if (task) {
dispatch_source_set_event_handler(self.timer,
task);
}
self.statues = GCDTimerNeedResume;
}
return self;
}
/**
恢复
*/
- (void)resume{
if (self.timer && (self.statues == GCDTimerNeedResume ||self.statues == GCDTimerSuspend)) {
dispatch_resume(self.timer);
self.statues = GCDTimerWorking;
}
}
/**
挂起
*/
- (void)suspend{
if (self.timer) {
dispatch_suspend(self.timer);
self.statues = GCDTimerSuspend;
}
}
- (void)cancle{
if (self.timer) {
dispatch_cancel(self.timer);
}
}
@end