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 = [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过去了.
转载于:https://www.cnblogs.com/Peak-Banish/p/4050288.html
iOS定时器NSTimer的使用方法相关推荐
- iOS定时器-- NSTimer 和CADisplaylink
iOS定时器-- NSTimer 和CADisplaylink 一.iOS中有两种不同的定时器: 1. NSTimer(时间间隔可以任意设定,最小0.1ms)// If seconds is les ...
- IOS 定时器 NSTimer
定时器对象,在OC中,定时器对象是NSTimer类型 //ViewController.h #import <UIKit/UIKit> @interface ViewController: ...
- iOS开发 NSTimer定时器
目录 前言 NSTimer是什么 NSTimer怎么使用 创建NSTimer 触发NSTimer 销毁NSTimer NSTimer与runloop NSTimer与performSelector:w ...
- iOS中定时器NSTimer的使用
1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...
- ios nstimer实现延时_iOS中定时器NSTimer的使用
1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...
- iOS之 NSTimer(一)
以前没怎么了解过这个NSTimer,其实还是有挺多坑的,今天来总结一下: 首先我们一起来看这个: 我在A -> (push) -> B控制器,然后再B控制器中开启了一个NSTimer.然 ...
- iOS开发中常用的方法
iOS开发中常用的方法 系统弹窗: 过期方法: UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"确认报价" ...
- IOS 计时器 NSTimer
1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...
- iOS 高效开发-----实现description 方法 (续)
在 "iOS 高效开发-----实现description 方法"这篇文章的最后,我留下了一个问题,就是,如果要给每个自定义的类覆写description方法和debugDescr ...
- 【H5】解决ios禁止缩放失效的方法
[H5]解决ios禁止缩放失效的方法 参考文章: (1)[H5]解决ios禁止缩放失效的方法 (2)https://www.cnblogs.com/gentle-Lee/p/12154924.html ...
最新文章
- 2022-2028年中国PGA树脂行业全景调研及投资前景展望报
- 文字识别技术在内容安全中的应用
- winform程序捕获全局异常,对错误信息写入日志并弹窗
- 如何让我们的vmware虚拟机上网!!
- 每列大于0的个数_二进制中1的个数(剑指offer第十四天)
- 思科cisco解决方案:思科ACI解决方案和Nexus_9000交换机
- BZOJ 1093 [ZJOI2007]最大半连通子图
- 无边框窗体的移动(winform/wpf)
- SAP Fiori Launchpad的后台配置路径
- MyBatis之基于XML的动态SQL
- HDU - 2102 A计划(双层BFS)
- Linux系统编程 -- IO缓冲区
- 曾经优秀的人,怎么就突然不优秀了。
- 测试工具ApiPost和Postman及Apifox
- mysql教程 check_Check
- Unet实现细胞分割
- 云主机概念的简单介绍
- Linux守护进程编写
- 海思平台OSD的实现(2)
- 环境法19春在线作业1-0003