NSTimer的使用
来源:互联网 发布:上古卷轴萝莉捏脸数据 编辑:程序博客网 时间:2024/06/07 04:53
一、NSTimer创建方式
1.通过[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(showAction) userInfo:nil repeats:YES];来创建
打印出来的日志如下
2016-09-03 08:16:32.512 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:16:35.512 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:16:38.513 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:16:41.513 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:16:44.513 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:16:47.513 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:16:50.513 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:16:53.514excersize[535:10412] ++++++++++++++++++++2016-09-03 08:16:56.514 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:16:59.513 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:17:02.513 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:17:05.513 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:17:08.514 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:17:11.514 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:17:14.514 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:17:17.513 excersize[535:10412] ++++++++++++++++++++2016-09-03 08:17:20.514 excersize[535:10412] ++++++++++++++++++++从打印的日志中可以看出该方法是不能实现精确定时的。 ];
2.通过//1.创建NSTimer
NSTimer * timer = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(showAction) userInfo:nil repeats:YES]; //2.把定时器添加到当前的RunLoop中,并选择NSRunLoopCommonModes 运行模式 [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
来创建。【注】在该种创建方式下必须将timer添加到相应的RunLoop模式下,否则timer不起作用。
二、精确定时GCD定时器
@interface ViewController ()/****定时器(这里不用带*,因为dispatch_source_t就是个类,内部已经包含了*)*****/@property(nonatomic,strong) dispatch_source_t timer;@end
-(void)gcdTimer{ //0.创建队列 dispatch_queue_t queue = dispatch_get_global_queue(0, 0); //1.创建一个GCD定时器 dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); self.timer = timer; //2.设置定时器开始时间、间隔时间、精准度 /** 第一个参数:要给哪个定时器设置 第二个参数:开始时间 第三个参数:间隔时间 第四个参数:精准度(误差) 一般为0 GCD的单位是纳秒 * */ dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 4 * NSEC_PER_SEC, 0 * NSEC_PER_SEC); //3.设置定时器要调用的方法 dispatch_source_set_event_handler(timer, ^{ NSLog(@"-------------------------"); }); //4.恢复启动定时器 dispatch_resume(timer);}
1 0
- NSTimer的使用
- NSTimer的使用
- NSTimer类的使用
- NSTimer的使用步骤
- NSTimer的使用
- NSTimer类的使用
- NSTimer的简单使用
- NSTimer的简单使用
- NSTimer的详细使用
- NSTimer类的使用
- NSTimer类的使用
- NSTimer的使用须知
- iOS NSTimer的使用
- NSTimer的使用
- NSTimer的使用须知
- NSTimer的使用Demo
- NSTimer的使用
- NSTimer 定时器的使用
- 《喵喵健康用户协议》
- H5音频
- 《java多线程编程核心技术》读书笔记1
- cartographer
- 【交互设计师必备书单】资深交互设计师推荐的交互设计师必看的七本书
- NSTimer的使用
- Maven 安装步骤
- poj 2531 Network Saboteur
- 杂谈:996有点可笑
- Hololens入门之手势识别(使用Manipulation gesture控制物体平移)
- MySQL忘记密码的正确解决方法
- C++ Primer读书笔记——第3章-字符串、向量和数组(上)
- 解决mac vim cscope快捷键映射无效问题
- 前端复习--css position属性