-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSTimer.m
84 lines (65 loc) · 2.5 KB
/
MSTimer.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
83
84
//
// MSTimer.m
// Miso
//
// Created by John Wu on 12/8/11.
// Copyright (c) 2011 TFM. All rights reserved.
//
#import "MSTimer.h"
@interface MSTimer () {
id _target;
SEL _selector;
NSTimer *_timer;
}
- (id) initTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats runLoop:(NSRunLoop *)runLoop mode:(NSString *)mode;
- (void)onTimer;
@end
@implementation MSTimer
+ (id) startTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats runLoop:(NSRunLoop *)runLoop mode:(NSString *)mode {
MSTimer *timer = [[[MSTimer alloc] initTimerWithTimeInterval:seconds target:target selector:aSelector userInfo:userInfo repeats:repeats runLoop:runLoop mode:mode] autorelease];
return timer;
}
- (id) initTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats runLoop:(NSRunLoop *)runLoop mode:(NSString *)mode {
self = [super init];
if (self) {
_target = target;
_selector = aSelector;
_timer = [[NSTimer timerWithTimeInterval:seconds target:self selector:@selector(onTimer) userInfo:userInfo repeats:repeats] retain];
[runLoop addTimer:_timer forMode:mode];
}
return self;
}
- (void)dealloc {
[_timer release];
_timer = nil;
_target = nil;
_selector = nil;
[super dealloc];
}
#pragma mark - Message Fowarding
+ (BOOL)instancesRespondToSelector:(SEL)aSelector {
return [[self class] instancesRespondToSelector:aSelector] || [[NSTimer class] instancesRespondToSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
if ([_timer respondsToSelector:[anInvocation selector]])
[anInvocation invokeWithTarget:_timer];
else
[super forwardInvocation:anInvocation];
}
- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
NSMethodSignature* signature = [super methodSignatureForSelector:selector];
if (!signature)
signature = [_timer methodSignatureForSelector:selector];
return signature;
}
- (BOOL) isKindOfClass:(Class)aClass {
return (aClass == [NSTimer class]) || [super isKindOfClass:aClass];
}
- (BOOL) respondsToSelector:(SEL)aSelector {
return [super respondsToSelector:aSelector] || [_timer respondsToSelector:aSelector];
}
#pragma mark - Private Methods
- (void)onTimer {
[_target performSelector:_selector];
}
@end