scheduledtimerwithtimeinterval怎么办

这篇文章将为大家详细讲解有关scheduledtimerwithtimeinterval怎么办,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

  以+scheduledTimerWithTimeInterval...的方式触发的timer,在滑动页面上的列表时,timer会暂定回调,为什么?如何解决?

  scheduledtimerwithtimeinterval怎么办

  RunLoop只能运行在一种mode下,如果要换mode,当前的loop也需要停下重启成新的。利用这个机制,ScrollView滚动过程中NSDefaultRunLoopMode(kCFRunLoopDefaultMode)的mode会切换到UITrackingRunLoopMode来保证ScrollView的流畅滑动:只能在NSDefaultRunLoopMode模式下处理的事件会影响ScrollView的滑动。

  如果我们把一个NSTimer对象以NSDefaultRunLoopMode(kCFRunLoopDefaultMode)添加到主运行循环中的时候,ScrollView滚动过程中会因为mode的切换,而导致NSTimer将不再被调度。

  同时因为mode还是可定制的,所以:

  Timer计时会被scrollView的滑动影响的问题可以通过将timer添加到NSRunLoopCommonModes(kCFRunLoopCommonModes)来解决。

  scheduledtimerwithtimeinterval代码如下:

  //将timer添加到NSDefaultRunLoopMode中

  [NSTimerscheduledTimerWithTimeInterval:1.0

  target:self

  selector:@selector(timerTick:)

  userInfo:nil

  repeats:YES];

  //然后再添加到NSRunLoopCommonModes里

  NSTimer*timer=[NSTimertimerWithTimeInterval:1.0

  target:self

  selector:@selector(timerTick:)

  userInfo:nil

  repeats:YES];

  [[NSRunLoopcurrentRunLoop]addTimer:timerforMode:NSRunLoopCommonModes];

关于“scheduledtimerwithtimeinterval怎么办”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。