最近有朋友问我使用NStimer遇见与ScrollView并存时存在主线程阻塞的问题,自己总结几种解决方法:


问题原因:

一般定时器timer都会被以默认模式default添加到主线程的runloop中,当scroll界面时,runloop接到信息处理事件,就会改变timer的运行模式到tracing,从而停止运行timer。


解决方法:

1、改变timer添加到runloop中的模式:

1 [[NSRunLoop currentRunLoop] addTimer:_shufflingFigureTimer forMode:NSRunLoopCommonModes];

2、把timer添加到支线程

1 [NSThread detachNewThreadSelector:@selector(bannerStart) toTarget:self withObject:nil];
2 - (void)bannerStart{
3          _shufflingFigureTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(shufflingFigureTimerAction:) userInfo:nil repeats:YES];
4         [[NSRunLoop currentRunLoop] addTimer:_shufflingFigureTimer forMode:NSDefaultRunLoopMode];
5         [[NSRunLoop currentRunLoop] run];
6 }

 

相关文章:

  • 2022-12-23
  • 2021-08-30
  • 2022-12-23
  • 2021-10-24
猜你喜欢
  • 2021-11-30
  • 2022-02-17
  • 2021-04-14
  • 2021-08-08
  • 2022-12-23
相关资源
相似解决方案