NSTimer扩展block版
序言
对于定时器,基本每个应用都使用需要到的,但是原生API使用起来并不是那么方便,还得处理各种回调,对于开发时相对复杂了。因此,尝试写下这个block
版本的,以简化调用!!!
头文件
下面是扩展的头文件,看看公开了哪些API:
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
|
//
// NSTimer+Convenience.h
// NSTimerBlockDemo
//
// Created by huangyibiao on 15/3/25.
// Copyright (c) 2015年 huangyibiao. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSTimer (Convenience)
/**
* 无参数无返回值Block
*/
typedef void (^HYBVoidBlock)(void);
/**
* 创建Timer---Block版本
*
* @param interval 每隔interval秒就回调一次callback
* @param repeats 是否重复
* @param callback 回调block
*
* @return NSTimer对象
*/
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
repeats:(BOOL)repeats
callback:(HYBVoidBlock)callback;
/**
* 创建Timer---Block版本
*
* @param interval 每隔interval秒就回调一次callback
* @param count 回调多少次后自动暂停,如果count <= 0,则表示无限次,否则表示具体的次数
* @param callback 回调block
*
* @return NSTimer对象
*/
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
count:(NSInteger)count
callback:(HYBVoidBlock)callback;
/**
* 开始启动定时器
*/
- (void)fireTimer;
/**
* 暂停定时器
*/
- (void)unfireTimer;
@end
|
实现文件
重点是实现文件,这里使用了非常巧妙的方式:让NSTimer
本身的userInfo
属性来存储回调!
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
|
//
// NSTimer+Convenience.m
// NSTimerBlockDemo
//
// Created by huangyibiao on 15/3/25.
// Copyright (c) 2015年 huangyibiao. All rights reserved.
//
#import "NSTimer+Convenience.h"
@implementation NSTimer (Convenience)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
repeats:(BOOL)repeats
callback:(HYBVoidBlock)callback {
return [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(onTimerUpdateBlock:)
userInfo:[callback copy]
repeats:repeats];
}
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
count:(NSInteger)count
callback:(HYBVoidBlock)callback {
NSDictionary *userInfo = @{@"callback" : [callback copy],
@"count" : @(count)};
return [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(onTimerUpdateCountBlock:)
userInfo:userInfo
repeats:YES];
}
+ (void)onTimerUpdateBlock:(NSTimer *)timer {
HYBVoidBlock block = timer.userInfo;
if (block) {
block();
}
}
+ (void)onTimerUpdateCountBlock:(NSTimer *)timer {
static NSUInteger currentCount = 0;
NSDictionary *userInfo = timer.userInfo;
HYBVoidBlock callback = userInfo[@"callback"];
NSNumber *count = userInfo[@"count"];
if (count.integerValue <= 0) {
if (callback) {
callback();
}
} else {
if (currentCount < count.integerValue) {
currentCount++;
if (callback) {
callback();
}
} else {
currentCount = 0;
[timer unfireTimer];
[timer invalidate];
timer = nil;
}
}
}
- (void)fireTimer {
[self setFireDate:[NSDate distantPast]];
}
- (void)unfireTimer {
[self setFireDate:[NSDate distantFuture]];
}
@end
|
代码中很关键的代码,获取回调,再获取已经回调了的次数,用于处理回调次数限制的情况:
1
2
3
4
5
|
NSDictionary *userInfo = timer.userInfo;
HYBVoidBlock callback = userInfo[@"callback"];
NSNumber *count = userInfo[@"count"];
|