1、NSProxy
@implementation FirViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(back)];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[[WeakProxy alloc] initWithTarget:self] selector:@selector(action:) userInfo:nil repeats:YES];
}
- (void)back
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)dealloc
{
[self.timer invalidate];
self.timer = nil;
NSLog(@"=== dealloc");
}
- (void)action:(id)sender
{
NSLog(@"====== %@",[NSDate date]);
}
@end
@implementation WeakProxy
{
__weak id _target;
}
- (WeakProxy*)initWithTarget:(id)target
{
_target = target;
return self;
}
-(NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
return [_target methodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
if ([_target respondsToSelector:invocation.selector]) {
[invocation invokeWithTarget:_target];
}
}
@end
2、GCD Timer
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
/*
参数1:_timer
参数2:开始时间
参数3:重复执行时间间隔
参数4:第四个参数 leeway 指的是一个期望的容忍时间,将它设置为 1 秒,意味着系统有可能在定时器时间到达的前 1 秒或者后 1 秒才真正触发定时器。在调用时推荐设置一个合理的 leeway 值。需要注意,就算指定 leeway 值为 0,系统也无法保证完全精确的触发时间,只是会尽可能满足这个需求。
*/
dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, 0), 0.1*NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(_timer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"执行了");
});
});
//开启计时器
dispatch_resume(_timer);
3、Block 封装
//3、block 处理循环引用,这两个方法放在 NSTimer Category 中
+ (NSTimer *)lhScheduledTimerWithTimeInterval:(NSTimeInterval)inerval
repeats:(BOOL)repeats
block:(void(^)(NSTimer *timer))block {
return [NSTimer scheduledTimerWithTimeInterval:inerval target:self selector:@selector(blcokInvoke:) userInfo:[block copy] repeats:repeats];
}
+ (void)blcokInvoke:(NSTimer *)timer {
void (^block)(NSTimer *timer) = timer.userInfo;
if (block) {
block(timer);
}
}