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

    • 这里强调一点:在主线程中以+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];
  • 相关阅读:
    parser_url
    fsockopen
    MySql支持的数据类型
    MySql常用字符集
    MySQL各大存储引擎
    MySql数据库基础
    Python 流程控制 超全解析(不可错过)
    python 序列解包(解压缩)
    python常量 (最全常量解析)
    python内存管理(通俗易懂,详细可靠)
  • 原文地址:https://www.cnblogs.com/huangzs/p/7574275.html
Copyright © 2011-2022 走看看