iOS中定时器NSTimer的使用

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 = [NSTimer  timerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:)userInfo:nilrepeats:NO];

[[NSRunLoop  currentRunLoop] 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过去了.

延时函数和Timer的使用

//延时函数:
[NSThread sleepForTimeInterval:5.0]; //暂停5s. //Timer的使用:
NSTimer *connectionTimer; //timer对象 //实例化timer
self.connectionTimer=[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop]addTimer:self.connectionTimer forMode:NSDefaultRunLoopMode];
//用timer作为延时的一种方法
do{
[[NSRunLoopcurrentRunLoop]runUntilDate:[NSDatedateWithTimeIntervalSinceNow:1.0]];
}while(!done); //timer调用函数
-(void)timerFired:(NSTimer *)timer{
done =YES;
}

转自:http://magicalboy.com/objective_c_nstimer_usage/#comment-45

创建 NSTimer

    1. Scheduled Timers & Using Selector
- (IBAction)startOneOffTimer:sender {
 
    [NSTimer scheduledTimerWithTimeInterval:2.0
 
             target:self
 
             selector:@selector(targetMethod:)
 
             userInfo:[self userInfo]
 
             repeats:NO];
 
}
      如上,如果没有重复执行的timer相当于

[self performSelector:@selector(targetMethod:) withObject:nil afterDelay:2.0];

// declcare
@property (assign) NSTimer *repeatingTimer;
 
// implements
- (IBAction)startRepeatingTimer:sender {
 
 
 
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5
 
                              target:self selector:@selector(timerFireMethod:)
 
                              userInfo:[self userInfo] repeats:YES];
 
    self.repeatingTimer = timer;
 
}
 
- (IBAction)stopRepeatingTimer:sender {
 
    [repeatingTimer invalidate];
 
    self.repeatingTimer = nil;
 
}
    1. Unscheduled Timers & Using Invocation
// declare
@property (retain) NSTimer *unregisteredTimer;
 
 
 
// implements
- (IBAction)createUnregisteredTimer:sender {
 
    NSMethodSignature *methodSignature = [self methodSignatureForSelector:@selector(invocationMethod:)];
 
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
 
    [invocation setTarget:self];
 
    [invocation setSelector:@selector(invocationMethod:)];
 
    NSDate *startDate = [NSDate date];
 
    [invocation setArgument:&startDate atIndex:2];
 
 
    NSTimer *timer = [NSTimer timerWithTimeInterval:0.5 invocation:invocation repeats:YES];
 
    self.unregisteredTimer = timer;
 
}
 
 
 
- (IBAction)startUnregisteredTimer:sender {
 
    if (unregisteredTimer != nil) {
 
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
 
        [runLoop addTimer:unregisteredTimer forMode:NSDefaultRunLoopMode];
 
    }
 
}
 
- (IBAction)stopUnregisteredTimer:sender {
 
    [unregisteredTimer invalidate];
 
    self.unregisteredTimer = nil;
 
}

定时器(NSTimer)的更多相关文章

  1. 定时器NSTimer的用法

        //时间间隔     NSTimeInterval activeTimeInterval = NETWORK_SEND_ACTIVE_TIME;     NSTimeInterval othe ...

  2. [置顶] ios 时间定时器 NSTimer应用demo

    原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9251917 demo功能:ios NSTimer应用demo .ipho ...

  3. iOS定时器-- NSTimer 和CADisplaylink

    iOS定时器-- NSTimer 和CADisplaylink 一.iOS中有两种不同的定时器: 1.  NSTimer(时间间隔可以任意设定,最小0.1ms)// If seconds is les ...

  4. ios基础篇(二十三)—— 定时器NSTimer与图片的自动切换

    一.NSTimer NSTimer是一个能在从现在开始到后面的某一个时刻或者周期性的执行我们指定的方法的对象.可以按照一定的时间间隔,将制定的信息发送给目标对象.并更新某个对象的行为.你可以选择在未来 ...

  5. IOS中定时器NSTimer的开启与关闭

    调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...

  6. ios - 图片自动轮播定时器(NSTimer)以及消息循环模式简介

    本文只是演示如何设置图片轮播的定时器. 创建全局变量NSTimer 程序启动后就开始轮播图片,所以在- (void)viewDidLoad中就启动定时器. 将定时器放入消息循环池中.- (void)v ...

  7. 定时器 NSTimer 和 CADisplayLink

    NSTimer *timer; CADisplayLink *caDisplayLink; int timeCount; - (void)viewDidLoad { [super viewDidLoa ...

  8. iOS定时器NSTimer的使用方法

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

  9. 【转】IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

随机推荐

  1. java系列-使用maven创建web项目(二)

    推荐2个maven找jar包配置的网站,只需要搜索关键字即可找到需要的Jar包,非常方便,比如:MySQL就可以找到mysql-connect-Java.jar. http://search.mave ...

  2. CentOS-6.5安装zabbix 3.0.4

    关闭selinux [root@localhost /]# sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux ...

  3. 第四章第四个例题(LRJ)

    半年了,最起码的编程能力也谈不上啊,思维神马就更不不敢说了. 互联网时代讲求效率,走得慢和不走没有区别了. The war is on. (buhuidetiduokanduodajibianyehu ...

  4. iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别

    触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponde ...

  5. iTool拷贝app到电脑上

    iTool拷贝app到电脑上 方法一. iTool找到你的app, 归档在桌面, 桌面就生成了ipa, 其实ipa是一个压缩包, 使用解压软件解压之后 生成Payload文件夹, 点开就可以看到Clo ...

  6. iOS的内购

    内购: 向苹果付钱购买与APP的使用相关的产品(游戏中的道具,装备等): 苹果将收到的钱按比例,转给APP方: 不同于APP中的第三方支付(不经过苹果):

  7. 二分图水一波~~~~d带你飞

    Current Time: 2016-03-11 17:45:36 Contest Type: Public Start Time: 2016-03-04 13:00:00 Contest Statu ...

  8. Python自动化之YAML解析

    准备工作 pip install PyYAML import yaml yaml语法规则 想要表示列表项,使用一个短横杠加一个空格.多个项使用同样的缩进级别作为同一列表的一部分 my_dictiona ...

  9. Python tab键自动补齐

    1.进入root家目录  建立.tab文件 .tab文件内容如下: ############################################## import sys import r ...

  10. 应用HTK搭建语音拨号系统2:创建单音素HMM模型

    选自:http://maotong.blog.hexun.com/6204849_d.html 苏统华 哈尔滨工业大学人工智能研究室 2006年10月30日 声明:版权所有,转载请注明作者和来源 该系 ...