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)

     

  • 相关阅读:
    python爬虫-execjs使用
    关于命令行操作数据库整理
    php项目整理之no1
    c++笔记整理
    php实战开发之自我整理(学习笔记)
    php之JavaScript
    html嵌入样式表
    php-css外边距
    The report for triangle problem
    An error in projects
  • 原文地址:https://www.cnblogs.com/beddup/p/4614664.html
Copyright © 2011-2022 走看看