以+scheduledTimerWithTimeInterval... 的方式触发的 timer,在滑动页面上的列表时, timer 会暂定回调,为什么?如何解决?
2016-07-17 08:56
411 查看
这里强调一点:在主线程中以+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:selfselector:@selector(run:)
userInfo:@"ya
了 个
hoo"repeats:YES];
// scheduledTimer...方法创建出来
NSTimer
虽然已经指定了默认模式,但是【允许你修改模式】
[[NSRunLoop currentRunLoop] addTimer:timerforMode:NSRunLoopCommonModes];
//
【仅在子线程】需要手动启动
RunLoop
对象,进入运行循环[[NSRunLoop currentRunLoop] run];
默认是运行在 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:selfselector:@selector(run:)
userInfo:@"ya
了 个
hoo"repeats:YES];
// scheduledTimer...方法创建出来
NSTimer
虽然已经指定了默认模式,但是【允许你修改模式】
[[NSRunLoop currentRunLoop] addTimer:timerforMode:NSRunLoopCommonModes];
//
【仅在子线程】需要手动启动
RunLoop
对象,进入运行循环[[NSRunLoop currentRunLoop] run];
相关文章推荐
- 重构一书问题笔记
- JavaScript传递变量:值传递?引用传递?
- Android总有那些你看了无数遍也记不住的简单知识点
- runloop 的 mode 作用是什么
- 今天早上起来也是够早的6:30
- PHP不使用?>结尾会更好!
- Apache在 prefork模式下,优化连接数
- selenium测试(Java)--关闭窗口(二十)
- 在Linux上挂载Windows共享文件夹,如何开机自动挂载(mount)?
- java高级特性之集合概述
- 角度与弧度的研究
- PHP读写大“二进制”文件,不必申请很大内存(fopen、fread、fwrite、fclose)
- 深入理解计算机系统chapter8
- 【PHP升级】CentOS6.3编译安装 PHP5.4.38
- 黄道、黄道平面、黄赤交角、正午太阳高度
- poj 1182 食物链(并查集的综合应用)
- PHP中file_exists与is_file、is_dir的区别,以及执行效率的比较
- java基础之三大特性 多态
- java基础之三大特性 类的继承和方法重写
- java基础之三大特性封装和隐藏