接口部分
@interface NSTimer (SGExtend)
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block;
+ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block;
@end
实现部分
@implementation NSTimer (Extend)
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block
{
NSTimer* timer = [NSTimer timerWithTimeInterval:interval target:self selector:@selector(timered:) userInfo:block repeats:repeats];
return timer;
}
+ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block
{
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timered:) userInfo:[block copy] repeats:repeats];
return timer;
}
+ (void)timered:(NSTimer *)timer
{
void (^block)(NSTimer *timer) = timer.userInfo;
block(timer);
}
@end
网友评论