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. 兼容ie的jquery ajax文件上传

    Ajax文件上传插件很多,但兼容性各不一样,许多是对ie不兼容的,另外项目中是要求将网页内容嵌入到桌面端应用的,这样就不允许带flash的上传插件了,如:jquery uploadify...悲剧 对 ...

  2. Mac os壁纸提取

    想必用过Mac os系统的朋友都知道mac壁纸,提取出来用作壁纸是十分好看的 下面给出壁纸的位置,自己用文件管理器打开然后复制出来即可! 系统盘:/Library/DesktopPictures

  3. BZOJ1503——郁闷的出纳员

    1.题目大意:一道treap题,支持插入,询问第K大,还有全体修改+上一个值,如果某个点值小于x,那么就删除这个点 插入100000次,询问100000次,修改100次..最后输出删了多少个点 2.分 ...

  4. DOM的相关优化

    为什么要进行DOM优化? DOM对象本身也是一个js对象,所以严格来说,并不是操作这个对象慢,而是说操作了这个对象后,会触发一些浏览器行为,比如布局(layout)和绘制(paint). 首先先说一些 ...

  5. PHP学习路线

    0x1 0x2

  6. am335x 虚拟机环境变量的设置及注释

    我用的还是老的Linux3.2.0的内核,只是借用了TI am335x SDK 3.0里面的工具. 首先下载TI官方的SDK,上一章已经安装了一个Ubuntu14.04的虚拟机. TI 最新的SDK下 ...

  7. BZOJ 2342: [Shoi2011]双倍回文

    Sol Manacher. 非常裸的Manacher啊...为什么有那么多人写Manacher+并查集?Set?Treap?...好神奇... 你只需要在 \(p[i]++\) 的位置加上判断就可以了 ...

  8. LED notification in Android device

    Code can control the LED notification in Android device, using android.app.Notification: 1 2 3 4 5 6 ...

  9. 3 EventTime 事件时间类和TimeNow函数——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 这里是时间相关类的第三个部分,也是最后一个部分. EventTime 事件时间类 这个类和Dela ...

  10. .Net Framework 3.5, 3.5 sp1 中文版离线安装

    安装.Net Framework 3.5和3.5 sp1时会自动下载语言包. 解决办法:下载语言包,将语言包拷贝到安装里. 1.用压缩工具解压.NetFramework3.5 2.将语言包文件拷贝到c ...