Created
September 9, 2011 15:40
-
-
Save justin/1206549 to your computer and use it in GitHub Desktop.
NSTimer+Blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef void (^SGBlock)(); | |
@interface NSTimer (Blocks) | |
+ (id)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)shouldRepeat block:(SGBlock)block; | |
+ (id)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)shouldRepeat block:(SGBlock)block; | |
@end | |
@implementation NSTimer (Blocks) | |
+ (id)scheduledTimerWithTimeInterval:(NSTimeInterval)theInterval repeats:(BOOL)shouldRepeat block:(SGBlock)theBlock | |
{ | |
SGBlock block = [theBlock copy]; | |
id ret = [self scheduledTimerWithTimeInterval:theInterval target:self selector:@selector(sg_fireBlock:) userInfo:block repeats:shouldRepeat]; | |
[block release]; | |
return ret; | |
} | |
+ (id)timerWithTimeInterval:(NSTimeInterval)theInterval repeats:(BOOL)shouldRepeat block:(SGBlock)theBlock | |
{ | |
SGBlock block = [theBlock copy]; | |
id ret = [self timerWithTimeInterval:theInterval target:self selector:@selector(sg_fireBlock:) userInfo:block repeats:shouldRepeat]; | |
[block release]; | |
return ret; | |
} | |
+ (void)sg_fireBlock:(NSTimer *)theTimer; | |
{ | |
if([theTimer userInfo]) | |
{ | |
SGBlock block = (SGBlock)[theTimer userInfo]; | |
block(); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment