This repository has been archived by the owner on Oct 14, 2018. It is now read-only.
/
NSTimer+JCSBlocks.m
63 lines (42 loc) · 1.72 KB
/
NSTimer+JCSBlocks.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
//
// NSTimer+JCSBlocks.m
// atime
//
// Created by Abizer Nasir on 26/08/11
// Copyright 2011 Jungle Candy Software. All rights reserved.
// Licences under the MIT Licence. See the README for details.
//
#import "NSTimer+JCSBlocks.h"
@interface JCSTimerBlockHandler : NSObject
@property (nonatomic, strong) id block;
- (void)jcs_handleBlockWithTimer:(NSTimer *)timer;
- (void)jcs_handleBlockWithInterruptableTimer:(NSTimer *)timer;
@end
@implementation NSTimer (JCSBlocks)
+ (NSTimer *)jcs_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats blockHandler:(JCSTimerCallback)block {
JCSTimerBlockHandler *blockHandler = [[JCSTimerBlockHandler alloc] init];
blockHandler.block = [block copy];
return [self scheduledTimerWithTimeInterval:seconds target:blockHandler selector:@selector(jcs_handleBlockWithTimer:) userInfo:nil repeats:repeats];
}
+ (NSTimer *)jcs_scheduledInterruptableTimerWithTimeInterval:(NSTimeInterval)seconds blockHandler:(JCSInterruptableTimerCallback)block{
JCSTimerBlockHandler *blockHandler = [[JCSTimerBlockHandler alloc] init];
blockHandler.block = [block copy];
return [self scheduledTimerWithTimeInterval:seconds target:blockHandler selector:@selector(jcs_handleBlockWithInterruptableTimer:) userInfo:nil repeats:YES];
}
@end
@implementation JCSTimerBlockHandler
@synthesize block;
- (void)jcs_handleBlockWithTimer:(NSTimer *)timer {
JCSTimerCallback callbackBlock = self.block;
callbackBlock();
}
- (void)jcs_handleBlockWithInterruptableTimer:(NSTimer *)timer {
JCSInterruptableTimerCallback callbackBlock = self.block;
BOOL stop;
callbackBlock(&stop);
if (stop) {
[timer invalidate];
timer = nil;
}
}
@end