Skip to content

Instantly share code, notes, and snippets.

@monjer
Last active December 20, 2015 06:38
Show Gist options
  • Select an option

  • Save monjer/6086704 to your computer and use it in GitHub Desktop.

Select an option

Save monjer/6086704 to your computer and use it in GitHub Desktop.
NSTimer block实现
//
// NSTimer+Block.h
// Created by manjun.han on 23/06/2013.
//
typedef void (^TimerBlock)(NSTimeInterval time);
@interface NSTimer (Block)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(TimerBlock) block repeats:(BOOL)repeats ;
@end
//
// NSTimer+Block.m
// Created by manjun.han on 23/06/2013.
//
#import "NSTimer+Block.h"
@interface NSTimer (BlockPrivite)
+ (void)exeBlockOfTimer:(NSTimer*)timer ;
@end
@implementation NSTimer (Block)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(TimerBlock) block repeats:(BOOL)repeats
{
return [self scheduledTimerWithTimeInterval:seconds target:self selector:@selector(exeBlockOfTimer:) userInfo:block repeats:repeats];
}
+ (void)exeBlockOfTimer:(NSTimer *)timer
{
NSTimeInterval time = [timer timeInterval];
TimerBlock block = [timer userInfo];
if (block){
block(time);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment