1、初始化

+ (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;

注:不用scheduled方式初始化的,需要手动addTimer:forMode: 将timer添加到一个runloop中。

  而scheduled的初始化方法将以默认mode直接添加到当前的runloop中.

举例:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];

NSTimer *myTimer = [NSTimertimerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:) userInfo:nilrepeats:NO];

[[NSRunLoopcurrentRunLoop] addTimer:myTimerforMode:NSDefaultRunLoopMode];

2、触发(启动)

当定时器创建完(不用scheduled的,添加到runloop中后,该定时器将在初始化时指定的timeInterval秒后自动触发。

可以使用-(void)fire;方法来立即触发该定时器;

注:You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

在重复执行的定时器中调用此方法后立即触发该定时器,但不会中断其之前的执行计划;

在不重复执行的定时器中调用此方法,立即触发后,就会使这个定时器失效。

3、停止

- (void)invalidate;

这个是唯一一个可以将计时器从runloop中移出的方法。

注:

NSTimer可以精确到50-100毫秒.

NSTimer不是绝对准确的,而且中间耗时或阻塞错过下一个点,那么下一个点就pass过去了.

使用代码例子:

在游戏循环中很好用的计时器
- (void)StartTimer
{
//repeats设为YES时每次 invalidate后重新执行,如果为NO,逻辑执行完后计时器无效
  [NSTimer  scheuledTimerWithTimeInterval:1.0/60.0  target: view  selector:@(timerAdvanced:) 
            userInfo:nil  repeats: YES];
}
 
- (void)timerAdvanced:(NSTimer *)timer//这个函数将会执行一个循环的逻辑
{
  //加一个计数器
  int  mTime;
  if (mTime == 0)
  {
    [timer invalidate];
    [self StartTimer];
  }
  mTime++;
  if(mTime >= 某个最大值MAXCOUNT)
    //处理某些逻辑
    。。。。。
    //在某处将 mTime重设为0
}
 

timerWithTimeInterval 方法详用的更多相关文章

  1. session的使用方法详解

    session的使用方法详解 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台WWW服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台WWW服务器 ...

  2. Kooboo CMS - Html.FrontHtml[Helper.cs] 各个方法详解

    下面罗列了方法详解,每一个方法一篇文章. Kooboo CMS - @Html.FrontHtml().HtmlTitle() 详解 Kooboo CMS - Html.FrontHtml.Posit ...

  3. HTTP请求方法详解

    HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源]     GET方法用来请求已被URI识别的资源.指定 ...

  4. ecshop后台增加|添加商店设置选项和使用方法详解

    有时候我们想在Ecshop后台做个设置.radio.checkbox 等等来控制页面的显示,看看Ecshop的设计,用到了shop_config这个商店设置功能 Ecshop后台增加|添加商店设置选项 ...

  5. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  6. C++调用JAVA方法详解

    C++调用JAVA方法详解          博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...

  7. windows.open()、close()方法详解

    windows.open()方法详解:         window.open(URL,name,features,replace)用于载入指定的URL到新的或已存在的窗口中,并返回代表新窗口的Win ...

  8. CURL使用方法详解

    php采集神器CURL使用方法详解 作者:佚名  更新时间:2016-10-21   对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程 ...

  9. JAVA 注解的几大作用及使用方法详解

    JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...

随机推荐

  1. 【Unity笔记】UGUI物体的Rect Transform组件(Pivot中心点,Anchor锚点)

    Pivot:自身中心点,图标是小蓝点.表示图片以哪个点来计算坐标值.默认在UI元素的几何中心点(0.5, 0,5). Anchor:锚点,图标是四个小三角形.表示该UI元素以父物体的哪个位置作为缩放参 ...

  2. 泛型方法前为什么要加<T>

    package com.test05.myTest; class Fruit { public String toString() { return "Fruit"; } } cl ...

  3. 中断线程详解(Interrupt)

    在上篇文章<多线程的使用——Thread类和Runnable接口>中提到中断线程的问题.在JAVA中,曾经使用stop方法来停止线程,然而,该方法具有固有的不安全性,因而已经被抛弃(Dep ...

  4. lvm 新建一个vg 逻辑卷 配置启动

    fdisk /dev/sdb 格式 t  8e w vgcreate datavg /dev/sdb1lvcreate -L 999G -n lvdata datavgmkfs.xfs /dev/da ...

  5. if no 和 if not

    x = None if x is not None: print("kong") if not x is not None: print("kong") # i ...

  6. 【Spring boot】第一个项目 Springboot + mysql + hibernate

    今天公司要做一个小项目,好久没碰项目了(刷题好累...),听说spring boot很火,决定试一试.暂时就从mysql里面读数据好了,使用hiberante. 1.获取jar包. 从http://s ...

  7. 让IE6支持min-height,max-height等的方法

    1.IE6支持max-height解决方法    IE6支持最大高度解决CSS代码:.yangshi{max-height:1000px;_height:expression((document.do ...

  8. 关于用Cocos2d-x.3.10运行别人游戏项目的步骤

    1.首先打开Cocos那个一体化软件. 2.创建工程,取名字,选择路径. 3.用VS2013打开新建的项目. 4.打开cocosdata(我自己放游戏项目的文件目录)的相应项目(自己刚创建的). 5. ...

  9. 【转】【C#】全局键盘监听

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...

  10. 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise

    题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...