码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-22 14:13:15      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:select   number   主线程   sele   with   track   des   方式   监听   

  • 这里强调一点:在主线程中以+scheduledTimerWithTimeInterval...的方式触发的timer默认是运行在NSDefaultRunLoopMode模式下的,当滑动页面上的列表时,进入了UITrackingRunLoopMode模式,这时候timer就会停止
  • 可以修改timer的运行模式为NSRunLoopCommonModes,这样定时器就可以一直运行了

  • 以下是我的笔记补充:

    • 子线程中通过scheduledTimerWithTimeInterval:...方法来构建NSTimer
      • 方法内部已经创建NSTimer对象,并加入到RunLoop中,运行模式为NSDefaultRunLoopMode
      • 由于Mode有timer对象,所以RunLoop就开始监听定时器事件了,从而开始进入运行循环
      • 这个方法仅仅是创建RunLoop对象,并不会主动启动RunLoop,需要再调用run方法来启动
    • 如果在主线程中通过scheduledTimerWithTimeInterval:...方法来构建NSTimer,就不需要主动启动RunLoop对象,因为主线程的RunLoop对象在程序运行起来就已经被启动了

      // userInfo参数:用来给NSTimer的userInfo属性赋值,userInfo是只读的,只能在构建NSTimer对象时赋值
      [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(run:) userInfo:@"ya了个hoo" repeats:YES];
      
      // scheduledTimer...方法创建出来NSTimer虽然已经指定了默认模式,但是【允许你修改模式】
      [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
      
      // 【仅在子线程】需要手动启动RunLoop对象,进入运行循环
      [[NSRunLoop currentRunLoop] run];

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

标签:select   number   主线程   sele   with   track   des   方式   监听   

原文地址:http://www.cnblogs.com/huangzs/p/7574275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!