文章来源:http://www.itnose.net/detail/6043298.html 更多文章:http://www.itnose.net/type/84.html
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;
注意:userInfo是值NSTimer携带的用户信息。
不用scheduled方式初始化的,需要手动addTimer:forMode: 将timer添加到一个runloop中。
而scheduled的初始化方法将以默认mode直接添加到当前的runloop中.
sample:
[NSTimer scheduledTimerWithTimeInterval:2 target:selfselector:@selector(startFindApartment:) userInfo:nil repeats:YES];
或
NSTimer *myTimer = [NSTimertimerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:) userInfo:nilrepeats:NO];
[[NSRunLoopcurrentRunLoop] addTimer:myTimerforMode:NSDefaultRunLoopMode];
2、触发(启动)
当定时器创建完(不用scheduled的,添加到runloop中后,该定时器将在初始化时指定的timeInterval秒后自动触发。
NSTimer *timer=[NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(timeSchedule) userInfo:nil repeats:YES];
NSRunLoop *runLoop=[NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
可以使用-(void)fire;方法来立即触发该定时器;
3、停止
- (void)invalidate;
这个是唯一一个可以将计时器从runloop中移出的方法。
4、在多线程开发中,如果是在mainthread中使用定时器,两种初始化方法都能使用,如果是在子线程中使用定时器,只能使用方法:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
_timer=[NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(timeSchedule) userInfo:nil repeats:YES];
NSRunLoop *runLoop=[NSRunLoop currentRunLoop];
[runLoop addTimer:_timer forMode:NSDefaultRunLoopMode];
while ([runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
相关推荐
1 使用注意事项: 1.1 倒计时时间间隔(时间单位是秒) 1.2 指定的执行方法 1.3 实现指定执行方法的对象 1.4 是否重复执行 2 对象的内存管理及销毁 2.1 使用方法” invalidate “进行停止 2.2 将对象设置为” nil ...
ios NSTimer应用demo 。iphone6.1 测试通过。具体demo截图和说明在:http://blog.csdn.net/donny_zhang
ios计时器NSTimer的使用 倒计时10秒,计时器 /* NSTimer scheduledTimerWithTimeInterval
iOS 中的 NSTimer.pdf
在APP内实现电子显示屏那样,对信息进行轮播播放。
简单的nstimer时间计算器,上面有暂停,清零等简单的功能,免费下载
使用系统原生的NSTimer来执行定时任务时会造成内存泄漏(闭环),同时在滑动ScrollView时,定时器并没起做用的bug Github:https://github.com/liuchongfaye/NSTimerInprovement 简书:...
1.UIScrollView循环滑动,滑动到最后一页时,继续滑动,回到第一页 2.开始手动滑动时,停止定时器,手动滑动结束后停止定时器 3.向前滑之后,自动向前滑动 4.向后滑动之后,自动向后滑动
详细的NSTimer类的使用 文档说明 是开发的很好的帮助文档
系统的NSTimer是添加到Runloop中的, 在系统繁忙时会造成偏差, 时间越长, 偏差越大 而MKImpulse是基于GCD编写的脉冲器, 精度由CPU时钟进行计算, 误差基本可以忽略不计 项目地址: MKImpulse 现已支持CocoaPods, ...
IOS应用源码——nstimer.zip
主要介绍了iOS中的NSTimer定时器的初步使用解析,通过例子简单讲解了NSTimer的输出与停止的方法,需要的朋友可以参考下
在 Controller B 中有一个 NSTimer @property (strong, nonatomic) NSTimer *timer; 你创建了它,并挂载到 main runloop self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector...
NStimer 定时器 iOS 倒计时 objective-c
关于定时器在tableview上的复用问题
在当前控制器(ViewController)的view上添加了一个自定义的view(LXFTimerView), LXFTimerView在成功创建出来后添加了定时器NSTimer并加入RunLoop开始工作, 当在当前控制器里将LXFTimerView移除掉后,定时器还在...