NSTimer 用法总结
来源:互联网 发布:网络上exm是什么意思 编辑:程序博客网 时间:2024/05/16 06:57
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过去了.
Timers的替代方法
如果只是要延迟消息的发送,可以使用NSObject的方法
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget
创建Timer的三种方法
1.scheduling a timer with the current run loop
2.creating a timer that you later register with a run loop
3.initializing a timer with a given fire date
Scheduled Timers
以下两个方法自动注册新创建的timer到当前NSRunLoop对象,NSRunLoop的模式为默认的NSDefaultRunLoopMode
- + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats
- + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
注:创建重复发送消息的timer一般需要保存一个引用,因为需要在某个时刻停止发送消息
Unscheduled Timers
创建未注册的timer,使用时调用addTimer:forMode注册到NSRunLoop对象
- timerWithTimeInterval:target:selector:userInfo:repeats:
- timerWithTimeInterval:invocation:repeats:
Initializing a Timer with a Fire Date
创建一个拥有指定发送日期的timerStopping a Timer
Memory Management
1. The run loop maintains the timer that is registered to it.
2. The timer is passed as an argument when you specify its method as a selector
3. You should maintain a strong reference to the unscheduled timer, in order to ensure that it's not deallocated before you use it.
4. A timer maintains a strong reference to its user info dictionary,
5. A timer maintains a strong reference to its target, so you should make sure that your timer's target survive longer than the timer itself.
- NSTimer 用法总结
- NSTimer用法总结
- NSTimer用法总结
- IOS NSTimer 定时器用法总结
- IOS NSTimer 定时器用法总结
- IOS NSTimer 定时器用法总结
- IOS开发 NSTimer 定时器用法总结
- 【转】IOS NSTimer 定时器用法总结
- NSTimer用法
- NSTimer总结
- NSTimer总结
- iOS中定时器NSTimer的一些用法总结
- NSTimer的用法
- NSTimer的用法
- NSTimer用法小结
- NSTimer 简单用法
- NSTimer的用法
- NSTimer的用法
- 数组简单总
- jplogicv1.0开发开发案例之知识库文档信息索引检索(类仿百度搜索、类google搜索)
- C++Primer学习笔记《2》
- netty学习之Channel接口
- ffmpeg(11) pmt_cb
- NSTimer 用法总结
- Netty Channel 接口名词理解
- Spring PropertyEditor实现绑定时的类型转换
- Springmvc PropertyEditor实现绑定时的类型转换
- OpenCv cv::Mat类用法
- ArcGIS Unable to Start service
- netty学习之ServerChannel
- STL 中的SET使用方法
- 懒人服务器 - 教程三、了解如何应用(DotNet客户端)