NSRunLoopCommonModes和Timer

来源:互联网 发布:mapreduce python编程 编辑:程序博客网 时间:2024/05/17 09:22

当使用NSTimer的scheduledTimerWithTimeInterval方法时。事实上此时Timer会被加入到当前线程的Run Loop中,且模式是默认的NSDefaultRunLoopMode。而如果当前线程就是主线程,也就是UI线程时,某些UI事件,比如UIScrollView的拖动操作,会将Run Loop切换成NSEventTrackingRunLoopMode模式,在这个过程中,默认的NSDefaultRunLoopMode模式中注册的事件是不会被执行的。也就是说,此时使用scheduledTimerWithTimeInterval添加到Run Loop中的Timer就不会执行。

所以为了设置一个不被UI干扰的Timer,我们需要手动创建一个Timer,然后使用NSRunLoop的addTimer:forMode:方法来把Timer按照指定模式加入到Run Loop中。这里使用的模式是:NSRunLoopCommonModes,这个模式等效于NSDefaultRunLoopMode和NSEventTrackingRunLoopMode的结合。
- (void)viewDidLoad
{
[super viewDidLoad];

NSLog(@"主线程 %@", [NSThread currentThread]);//创建TimerNSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timer_callback) userInfo:nil repeats:YES];//使用NSRunLoopCommonModes模式,把timer加入到当前Run Loop中。[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

}

//timer的回调方法
- (void)timer_callback
{
NSLog(@”Timer %@”, [NSThread currentThread]);
}

0 0
原创粉丝点击