美文网首页
NSTimer 简单使用方法

NSTimer 简单使用方法

作者: 7分醉 | 来源:发表于2016-03-28 16:46 被阅读214次

前言

NSTimer一般用来定时,以便出发需要周期性执行的任务或者间隔一定时间后执行的任务。

使用方法

常用的启动定时器的方法有两种:

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

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

停止方法

//通过invalidate方法可以停止定时器的工作,一旦定时器被停止了,就不能再次执行任务。
//只能再创建一个新的定时器才能执行新的任务
- (void)invalidate;

简便的方法(第一种)

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti 
target:(id)aTarget 
selector:(SEL)aSelector 
userInfo:(nullable id)userInfo 
repeats:(BOOL)yesOrNo;
参数:
(NSTimeInterval)ti  :延时时间(double类型)
target:(id)aTarget  :监听时钟触发的对象
selector:(SEL)aSelector :时钟触发的方法
userInfo:(nullable id)userInfo :可以是任意对象,通常为nil
repeats:(BOOL)yesOrNo :是否重复触发(为YES时,每隔一定的延时时间就会触发方法,为NO时,从现在开始间隔一定的延时时间就会触发,但只触发一次)

是简便的方法,使用示例如下:

//每隔1秒就会触发updateTime方法
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

//定时器触发方法
- (void)updateTime
{
   //定时时间到后会触发改方法
}

这种方法使用时有弊端,就是当界面有滚动监听时(如有一个UITextView),到了延时时间并不会去执行触发方法,等停止滚动时才会执行触发方法。
这就需要第二种启动定时器的方法。

第二种启动定时器的方法

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

参数:
(NSTimeInterval)ti  :延时时间(double类型)
target:(id)aTarget  :监听时钟触发的对象
selector:(SEL)aSelector :时钟触发的方法
userInfo:(nullable id)userInfo :可以是任意对象,通常为nil
repeats:(BOOL)yesOrNo :是否重复触发(为YES时,每隔一定的延时时间就会触发方法,为NO时,从现在开始间隔一定的延时时间就会触发,但只触发一次)

使用示例:

self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
// 将timer添加到运行循环
// 模式:NSRunLoopCommonModes的运行循环模式(监听滚动模式)
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

NSRunLoopCommonModes模式为监听滚动模式,将定时器加入该模式就和滚动触发事件在同一个等级,这样就不会受滚动事件的影响了。

另外还有一种模式,DefaultRunLoopMode,默认模式,加入这种模式就和第一种方法的效果相同了。

总结

所以说使用NSTimer来定时其定时有时候会不准确,通常使用NSTimer来触发时间间隔相对较长,且如果不准确对后果无太大影响的事件。
如果要出发时间间隔较短的事件可以使用CADisplayLink CADisplayLink参考

相关文章

  • NSTimer使用:暂停、继续、释放、解除循环引用

    NSTimer的使用方法: 1、初始化: + (NSTimer *)timerWithTimeInterval:(...

  • NSTimer总结

    NSTimer使用方法 初始化+ (NSTimer)timerWithTimeInterval:(NSTimeIn...

  • NSTimer 简单使用方法

    前言 NSTimer一般用来定时,以便出发需要周期性执行的任务或者间隔一定时间后执行的任务。 使用方法 常用的启动...

  • NSTimer收录

    使用方法: timeMedia = [NSTimer scheduledTimerWithTimeInterval...

  • iOS中解决NSTimer循环引用问题

      NSTimer使用不当就会造成内存泄漏,比如常见的使用方法:   由于NSTimer会引用住self,而 se...

  • NSTimer 的使用

    使用方法 scheduledTimerWith和timerWith和区别 NSTimer是加到runloop中执行...

  • NSTImer

    NSTimer 基础请参考: NSTimer的使用以及 史上最简单的,NSTimer暂停和继续 Firing a ...

  • NSTimer的使用

    NSTimer 的使用 为什么会写NSTimer呢? 原因很简单, 这里有坑! NSTimer 使用的顺序 创建N...

  • NSTimer

    创建NSTimer创建NSTimer的常用方法是 创建NSTimer的不常用方法是 和 他们的区别很简单: 所以说...

  • NSTimer

    用法 NSTimer的用法很简单,个人最常用的是下面这个方法。 [NSTimer scheduledTimerWi...

网友评论

      本文标题:NSTimer 简单使用方法

      本文链接:https://www.haomeiwen.com/subject/hqbjlttx.html