Skip to content

Instantly share code, notes, and snippets.

@leeprobert
Created October 9, 2011 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leeprobert/1273709 to your computer and use it in GitHub Desktop.
Save leeprobert/1273709 to your computer and use it in GitHub Desktop.
Category on NSTimer that adds Blocks support.
#import <Foundation/NSTimer.h>
@interface NSTimer (PSYBlockTimer)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats usingBlock:(void (^)(NSTimer *timer))fireBlock;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats usingBlock:(void (^)(NSTimer *timer))fireBlock;
@end
#import "NSTimer+Blocks.h"
typedefvoid (^PSYTimerBlock)(NSTimer *);
#define SELF_EXECUTING 1
#if defined(SELF_EXECUTING) && SELF_EXECUTING
@interface NSTimer (PSYBlockTimer_private)
+ (void)PSYBlockTimer_executeBlockWithTimer:(NSTimer *)timer;
@end
@implementation NSTimer (PSYBlockTimer)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats usingBlock:(void (^)(NSTimer *timer))fireBlock
{
return [selfscheduledTimerWithTimeInterval:seconds target:selfselector:@selector(PSYBlockTimer_executeBlockWithTimer:) userInfo:[[fireBlock copy] autorelease] repeats:repeats];
}
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats usingBlock:(void (^)(NSTimer *timer))fireBlock
{
return [selftimerWithTimeInterval:seconds target:selfselector:@selector(PSYBlockTimer_executeBlockWithTimer:) userInfo:[[fireBlock copy] autorelease] repeats:repeats];
}
@end
@implementation NSTimer (PSYBlockTimer_private)
+ (void)PSYBlockTimer_executeBlockWithTimer:(NSTimer *)timer
{
PSYTimerBlock block = [timer userInfo];
block(timer);
}
@end
#else
// Private helper class
__attribute__((visibility("hidden")))
@interface _PSYBlockTimer : NSObject { PSYTimerBlock block; }
+ (id)blockTimer;
@property(copy) PSYTimerBlock block;
- (void)executeBlockWithTimer:(NSTimer *)timer;
@end
@implementation NSTimer (PSYBlockTimer)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats usingBlock:(void (^)(NSTimer *timer))fireBlock
{
_PSYBlockTimer *blkTimer = [_PSYBlockTimer blockTimer];
[blkTimer setBlock:fireBlock];
return [self scheduledTimerWithTimeInterval:seconds target:blkTimer selector:@selector(executeBlockWithTimer:) userInfo:nil repeats:repeats];
}
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats usingBlock:(void (^)(NSTimer *timer))fireBlock
{
_PSYBlockTimer *blkTimer = [_PSYBlockTimer blockTimer];
[blkTimer setBlock:fireBlock];
return [self timerWithTimeInterval:seconds target:blkTimer selector:@selector(executeBlockWithTimer:) userInfo:nil repeats:repeats];
}
@end
@implementation _PSYBlockTimer
@synthesize block;
+ (id)blockTimer { return [[[self alloc] init] autorelease]; }
- (void)executeBlockWithTimer:(NSTimer *)timer
{
block(timer);
}
- (void)dealloc
{
[block release];
[super dealloc];
}
@end
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment