比如,定义一个NSTimer来隔一会调用某个方法,但这时你在拖动textVIew不放手,主线程就被占用了。timer的监听方法就不调用,直到你松手,这时把timer加到 runloop里,就相当于告诉主循环腾出点时间来给timer,再拖动textView就不会因主线程被占用而不调用了。

 

   NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    
    // 定时器只运行在NSDefaultRunLoopMode下,一旦RunLoop进入其他模式,这个定时器就不会工作
     [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

 

改为:

   // 定时器会跑在标记为common modes的模式下
    // 标记为common modes的模式:UITrackingRunLoopMode和kCFRunLoopDefaultMode
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

 

相关文章:

  • 2021-04-11
  • 2022-03-01
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2021-10-09
  • 2022-12-23
  • 2021-05-01
  • 2022-12-23
相关资源
相似解决方案