变更记录

序号 录入时间 录入人 备注
1 2015-03-25 Alfred Jiang -
2 2015-12-23 Alfred Jiang -
2 2016-01-10 Alfred Jiang 更新 TTGCDTimerWrapper 实现方式

方案名称

时间 - 倒计时器的实现

关键字

时间 \ 倒计时 \ 计时器 \ 延时执行

需求场景

  1. 倒计时
  2. 延时执行

参考链接

  1. GitHub - TTGCDTimerWrapper

详细内容

#####1. 方法定义(注:对 TTGCDTimerWrapper 源代码做了少许修改)

TTGCDTimerWrapper.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#import <Foundation/Foundation.h>

@interface TTGCDTimerWrapper : NSObject

//Either a dispatch queue is provided or a default dispatch queue
//will be used. (dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);)
- (id)initWithTargetQueue:(dispatch_queue_t)targetQueue;

//Will perform a block on the provided queue (or default queue) after a given interval.
//One can also set it to repeat.
- (void)afterInterval:(NSTimeInterval)interval perform:(void (^)())block repeats:(BOOL) repeats;

//Will cancel the timer and if needed the targetQueue is released.
- (void)cancel;

@end

TTGCDTimerWrapper.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
#import "TTGCDTimerWrapper.h"

@implementation TTGCDTimerWrapper {
dispatch_queue_t _queue;
dispatch_source_t _timer;
}

- (id)initWithTargetQueue:(dispatch_queue_t)targetQueue {
self = [super init];

if(self) {
_queue = targetQueue ?:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _queue);
}
return self;
}

- (void) afterInterval: (NSTimeInterval)interval perform:(void (^)())block repeats:(BOOL) repeats {
[self performSync: ^{

NSTimeInterval timeIntervalInNSec = interval * NSEC_PER_SEC;

dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, timeIntervalInNSec), timeIntervalInNSec, 0);

dispatch_source_set_event_handler(_timer, ^{
if(block){
block();
}
if (!repeats) {
[self cancel];
}
});

dispatch_resume(_timer);
}];
}

- (void)performSync: (dispatch_block_t)block {
dispatch_sync(_queue, block);
}

- (void)cancel {
[self performSync: ^{
if(_timer) {
dispatch_source_cancel(_timer);

#if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
dispatch_release(_timer);
#endif
_timer = NULL;
}
}];
}

- (void)dealloc {
[self cancel];
_queue = NULL;
}

@end

#####2. 示例

  • 注意点 1 :一定要声明为成员变量
  • 注意点 2 :无论是否 ARC, 一定要在 dealloc 中执行 cancel 方法
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
//
// ViewController.m
// testproject
//
// Created by viktyz on 16/1/10.
// Copyright © 2016年 Alfred Jiang. All rights reserved.
//

#import "ViewController.h"
#import "TTGCDTimerWrapper.h"

@interface ViewController ()
{
TTGCDTimerWrapper *timer; //注意点 1 :一定要声明为成员变量
}

@end

@implementation ViewController

- (void)dealloc
{
[timer cancel]; //注意点 2 :无论是否 ARC, 一定要在 dealloc 中执行 cancel 方法
}

- (void)viewDidLoad
{
[super viewDidLoad];

timer = [[TTGCDTimerWrapper alloc] initWithTargetQueue:nil];

[timer afterInterval:5.0
perform:^{
NSLog(@"After 5 Seconds");
}
repeats:YES];
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

效果图

(无)

备注