zoukankan      html  css  js  c++  java
  • iOS开发总结(A0) - NStimer

    NStimer是ios开发的计时器,简单易用,但有几个注意事项

    1. 创建NStimer的两个常用方法是

    + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

    第一个创建的timer,需要手动加到runloop中去,否则timer 不起作用,加到runloop的方法是:

        [[NSRunLoop mainRunLoop] addTimer:timer forMode:RunLoopMode];

    或者

        [[NSRunLoop currentRunLoop] addTimer:timer forMode:RunLoopMode];

    mainRunloop和currentRunLopp的区别是

    - mainRunloop是主线程的runloop

    - currentRunLopp是当前线程的runloop

    RunLoopMode有两种:NSDefaultRunLoopMode和NSRunLoopCommonModes,区别:

    - NSDefaultRunLoopMode, 如tableview在滚动时,timer失效

    NSRunLoopCommonModes,tableview滚动时,timer不失效

    更多信息,参考 

    http://www.cnblogs.com/zhangjie/p/3727469.html

    http://www.cnblogs.com/zhangjie/p/3727485.html

    第二个创建的timer,系统自动加到runloop中去( current run loop in the default mode)

    2. timer 是retain target 的,只有timer invalidate后,才释放target. 

    对于不重复的timer,执行一次之后timer自动失效,对于重复的timer,需要手动[timer invalidate];

     

    3. timer 时间间隔是大概的时间,要准确及时,可使用CADisplayLink(参考http://www.cnblogs.com/YouXianMing/p/4029547.html)和 GCD(后续将专门学习总结GCD)

     

  • 相关阅读:
    讲课专用——线段树——最长上升子序列
    讲课专用——线段树——最长连续空位
    讲课专用——线段树——BSS
    求背景图
    致备战noip2018的勇士
    TNS-12547 Linux Error: 104: Connection reset by pe (转载)
    清理Oracle安装目录里的一些日志信息
    tmp_table_size
    11gr2 alert日志中报TNS-12535 TNS-00505原因及解决方法 (转载)
    systemstate dump 介绍
  • 原文地址:https://www.cnblogs.com/beddup/p/4614664.html
Copyright © 2011-2022 走看看