创建定时器
在NSTimer类中有几种创建定时器的方法:
// 类方法
+ (NSTimer *)timerWithTimeInterval:target:selector:userInfo:repeats:
+ (NSTimer *)scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
// 实例方法
- (instancetype)initWithFireDate:interval:target:selector:userInfo:repeats:
要想让定时器真正的工作,需要把定时器加入到RunLoop中,需要注意的是,在子线程的RunLoop中添加定时器需要自己创建一个和该子线程关联的RunLoop.当你在子线程中调用[NSRunLoop currentRunLoop]时,会帮你创建一个RunLoop.
// 实例方法创建定时器
NSDate *futureDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
NSTimer *timer = [[NSTimer alloc] initWithFireDate:futureDate
interval:0.1
target:self
selector:@selector(calledSelector:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] timer forMode:NSDefaultRunLoopMode];
// timerWithTimeInterval类方法创建定时器
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:@selector(calledSelector:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
// scheduledTimerWithTimeInterval类方法创建对象
[NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(calledSelector:)
userInfo:nil
repeats:YES];
在上面几个方法中,只有scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:方法不需要将timer添加到NSRunLoop上面,因为这个方法默认将timer添加到了NSRunLoop上,并且默认是NSDefaultRunLoopMode.而另外两个方法需要手动添加到NSRunLoop并指定模式.
当不想添加到默认模式的时候可以使用第二种方式创建定时器对象.这个需求通常在滚动UI的时候,想要使定时器继续工作,便需要把timer添加到NSRunLoop的NSRunLoopCommonModes上.
NSRunLoopCommonModes是一组可配置的通用模式,对于Cocoa应用,该模式缺省的包含了default,modal以及event tracking(即UITrackingRunLoopMode)模式.
开启一个
NSTimer实质上是在当前的RunLoop中配置了一个定时源.每次循环都会检测timer是否可以出发,当RunLoop处于A模式,注册在B模式的timer就无法被检测到了.因此当滑动一个scrollView时,注册在NSDefaultRunLoopMode上的timer是无法被UITrackingRunLoopMode模式检测到的.因此在UITrackingRunLoopMode模式下也要注册一下timer,才能让定时器依照我们的期望正常工作.
如果我想用scheduledTimerWithTimeInterval类方法创建一个在滑动模式下计时的定时器,该怎么做呢?
由于scheduledTimerWithTimeInterval:target:selector:userInfo:repeats默认将定时器配置到了NSRunLoop上,并且默认是NSDefaultRunLoopMode,想要在滑动模式下也能计时,需要使用addTimer:forMode:方法添加滑动换模式.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(calledSelector:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
// 或者[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
很简单~再看一个问题.
如果我想用scheduledTimerWithTimeInterval类方法创建一个只在滑动模式下计时的定时器,该怎么做呢?
注意,这个问题比上一个问题多了一个只字.这下麻烦了,scheduledTimerWithTimeInterval:target:selector:userInfo:repeats方法默认添加到了NSDefaultRunLoopMode,头文件里面也没有提供更改或删除mode的方法.如果我们使用[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];,实际上是让这个timer配置到了两种mode下.如何删除其中一个mode呢? 我使用了Xtracedump了NSTimer的私有方法,成功地将timer从NSDefaultRunLoopMode中移除.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(calledSelector:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
[[NSRunLoop currentRunLoop] performSelector:@selector(removeTimer:forMode:)
withObject:timer
withObject:NSDefaultRunLoopMode];
虽然这操作然并卵,但是对理解RunLoop还是有帮助的.










网友评论