翻译NSTimer官方原文,包括初始化,启动,停止NSTimer

来源:互联网 发布:win10 映射网络驱动器 编辑:程序博客网 时间:2024/06/03 05:06

*说明:翻译水平有限,希望对初学者有帮助。本人添加的部分用下划线标明重点用橙色字体


类 NSTimer 说明

一、概述

        你可以用NSTimer类来创建定时器对象。经过一定时间启动定时器,并发送一个特定消息给目标对象。比如:你创建了一个定时器,用于一段时间之后,给窗口发了一条消息,告诉它更新。

        定时器跟run loops协同工作。想要高效使用NSTimer,你应该了解run loops是怎么操作的。需要特别说明的是:run loops 持有(retain)定时器。所以,你可以在定时器加入run loop后,释放它。

        定时器不是真正意义上,实时生效的机制;仅当run loop(自己已经添加进去了)正在运行,且程序设定的启动时间已经过去了,定时器才会启动。由于run loop管理着各种的输入源,所以NSTimer的精准度在50-100微秒。如果定时器启动时刻正一个很长时间的callout或者run loop处于一个不再检测这个定时的模式,它将不启动直到run loop检测到下一个启动时刻。因此,定时器真正生效时间,应该在程序设定启动时刻之后的一段时间内。

        NSTimer 是“tool-free bridge”模式,在Core Foundation中对应着CFRunLoopTimerRef。

重复 vs 非重复定时器

        在初始化定时间的时候,你要指定这个定时器是重复的还是非重复的。顾名思义:非重复的定时器启动一次后,自动失效,从而防止再次触发定时器。与此相反,重复定时器启动一次后,又把自己放到run loop里,准备下次启动。

        重复模式的定时器总是根据,被设计好的时间来安排自己启动时间,并非实际启动时间。如果,一个定时器的启动时间是每5秒。那么定时器就在5秒后启动,即便实际的时间被延时了。如果,启动时间呗延时的足够长,达到多个启动时间(10秒,20秒等),这段时间内定时器将只启动一次;定时器被重新排布时间,等待下次启动时刻到来。

在Run Loops里安置定时器

一个定时器对象,一次只能被注册到一个run loop,虽然它能被添加到“multiple run loop”借助与它所在的run loop
有三种方法创建一个定时器。
1.使用scheduledTimerWithTimeInterval:invocation:repeats: 或者scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:来创建定时器,默认被安置到当前的run loop上。
2.使用 timerWithTimeInterval:invocation:repeats: 或者 timerWithTimeInterval:target:selector:userInfo:repeats: 来创建定时器,不会安置到run loop里(创建后,你必须手动安置到一个run loop里,通过调用NSRunLoop的函数 addTimer:forMode: )。
3.开辟资源并初始化定时器用 initWithFireDate:interval:target:selector:userInfo:repeats:方法。(同样,你必须手动安置到一个run loop里通过调用NSRunLoop的函数 addTimer:forMode: 

   一旦定时器被安置到run loop里,定时器就开运行了,直到自己失效。非重复定时器运行后,就让自己失效。然而,对于重复模式定时器,你需要调用invalidate函数使定时器失效。调用invalidate使得定时器从当前的run loop移除。结果,你应该总是调用 invalidate 方法,来移除安装在同一个线程上的定时器。失效的定时器会立即被禁用,以便它不再影响run loop,run loop在invalidate 方法返回前或者稍晚一些,移除并释放定时器,定时器一旦失效,这个对象将不能再使用了。

NSTimer子类注意事项

       你不应该尝试用NSTimer生成子类。


Tasks

--------------------------------------------------------------------------
创建定时器:
  • + scheduledTimerWithTimeInterval:invocation:repeats:(下面有详细解析)
  • + scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
  • + timerWithTimeInterval:invocation:repeats:
  • + timerWithTimeInterval:target:selector:userInfo:repeats:
  • – initWithFireDate:interval:target:selector:userInfo:repeats:
启动定时器:
– fire
停止定时器
– invalidate(不可恢复
暂停定时器

[timersetFireDate:[NSDatedistantFuture]];

恢复定时器

[timersetFireDate:[NSDatedate]];

[iNorSlidePer fire];


其他信息
  • – isValid
  • – fireDate
  • – setFireDate:
  • – timeInterval
  • – userInfo

类方法

-------------------------------------------------------------------

scheduledTimerWithTimeInterval:invocation:repeats:

创建一个NSTimer对象,默认并把它安置到当前的run loop里。
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats
参数:
seconds:定时器间隔,如果seconds <= 0.0,该函数选择0.1毫秒代替。
invocation:The invocation to use when the timer fires. The timer instructs the invocation object to retain its arguments.
repeats:如果YES,定时器重复启动自己直到失效。如果NO,定时器启动后即失效。
返回值:NSTimer对象。
讨论:经过seconds秒后,定时器启动,回调invocation。

scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

创建一个NSTimer对象,默认并把它安置到当前的run loop里。
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelectoruserInfo:(id)userInfo repeats:(BOOL)repeats
参数:
seconds:定时器间隔,如果seconds <= 0.0,该函数选择0.1毫秒代替。
target:定时器启动后,发送消息的对象。定时器保存target对象并释放target当自己无效。
aSelector:定时器启动后发送消息给target,selector 的格式必须是返回void并带一个参数。定时器传递把自己当做参数传给aSelector。
userInfo:用于定时器的use info,定时器保存这个对象,并释放它当定时器无效后。该参数可以为nil
repeats:如果YES,定时器重复启动自己直到失效。如果NO,定时器启动后即失效。
返回值:NSTimer对象
讨论:seconds秒之后,定时器启动。发送一个aSelector的消息给target。

timerWithTimeInterval:invocation:repeats:

创建一个用invocation对象初始化的NSTimer对象,
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats
参数:
seconds(同上)
invocation(同上)
repeats(同上)
返回值(同上)
讨论:你必须调用 addTimer:forMode:把定时器对象添加到一个run loop里。经过seconds后,定时器启动,回调invocation。(如果是重复模式定时器,没有必要重复添加到run loop里)

timerWithTimeInterval:target:selector:userInfo:repeats:

创建一个用特定对象初始化的NSTimer对象。
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
参数:
seconds:(同上)
target:(同上)
aSelector:定时器启动后发送消息给target,aSelector必须是如下函数签名
- (void)timerFireMethod:(NSTimer*)theTimer
定时器把自己当做参数传递给这个函数。
userInfo: 用于定时器的use info,定时器保存这个对象,并释放它当定时器无效后。该参数可以为nil
repeats:(同上)
返回值:NSTimer对象
讨论:你必须调用 addTimer:forMode:把定时器对象添加到一个run loop里。经过seconds后,定时器启动,回调invocation。(如果是重复模式定时器,没有必要重复添加到run loop里)


方法实例

----------------------------------------------------------------------------------------------------------------------------------------------------
fire
触发定时器,发送消息给target。
- (void)fire
讨论
你可以用这个方法启动一个重复模式定时器。如果该定时器是非重复模式,那么它也自动失效当fire后,及时还没到程序设定的启动时刻。

fireDate
返回定时器启动的日期。
- (NSDate *)fireDate
返回值
定时器启动的日期,如果定时器是无效的,该方法返回定时器最后一次启动的日期。

initWithFireDate:interval:target:selector:userInfo:repeats:

用target和select初始化定时器。
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelectoruserInfo:(id)userInfo repeats:(BOOL)repeats
参数:
date
        定时器启动的日期。
seconds:(同上)
target:(同上)
aSelector:定时器启动后发送消息给target,aSelector必须是如下函数签名
- (void)timerFireMethod:(NSTimer*)theTimer
定时器把自己当做参数传递给这个函数。
userInfo :(同上 )
repeats:(同上)
返回值:如果该定时器被添加到run loop里,并且它又是一个重复模式,它将在date的时候启动,每秒钟都启动一次。
讨论:你必须调用 addTimer:forMode:把定时器对象添加到一个run loop里。经过seconds后,定时器启动,回调invocation。(如果是重复模式定时器,没有必要重复添加到run loop里)

invalidate

停止定时器,并将其从run loop里移除。无法再恢复,再次使用

- (void)invalidate
讨论
    该方法是将定时器从run loop里移除的唯一方法。run loop在invalidate 方法返回前或者稍晚一些,移除并释放定时器。
If it was configured with target and user info objects, the receiver releases its references to those objects as well.
特别注意事项:

你必须在定时器所在的线程中发送这个消息。如果从其他可能退出的线程中发送这个消息,定时器相关的输入源可能会从run loop中移除。

isValid

返回布尔类型,表明定时器是有效还是无效的。
- (BOOL)isValid

返回值:如果YES表示定时器还可以启动,如果NO说明定时器已经无效了,不能再启动了。

setFireDate:


重置定时器启动的日期。
- (void)setFireDate:(NSDate *)date
参数:
date:定时器启动的日期。如果这个日期是过去式,那么该方法就设置当前时间作为启动日期。
讨论
        通常,你用这个方法来调整重复模式定时器的启动时间。尽管重置定时器日期是个相对开销较大的操作,但是有些时候它很更有效。比如:你可以在将来一段时间内规律得重复一个动作很多次这种情况下使用它。调整一个定时器将比创建多个定时器对象,安置到run loop并销毁他们这些复杂操作的开销要小。
        当定时器无效时,还有非重复定时器启动后,不应该调用该方法。你可以在非重复定时器启动前调用该方法,但是你要避开该定时器所在线程中潜在的竞争条件。

timeInterval


返回定时器的启动时间间隔
- (NSTimeInterval)timeInterval
返回值:
     定时器的启动时间间隔。如果是非重复定时器,即便它设置了启动时间,但是也返回0

userInfo

返回定时器的userInfo对象
- (id)userInfo
返回值“
     定时器的userInfo对象
讨论:
     当定时器无效后,不要调用该方法。用isValid检测定时器是否已经无效。

(完)