ios 重复执行方法

来源:互联网 发布:linux下怎样输入中文 编辑:程序博客网 时间:2024/05/18 03:49
一、NSTimer
1. 创建方法
1
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(action:) userInfo:nil repeats:NO];
  • TimerInterval : 执行之前等待的时间。比如设置成1.0,就代表1秒后执行方法
  • target : 需要执行方法的对象。
  • selector : 需要执行的方法
  • repeats : 是否需要循环
2. 释放方法
1
[timer invalidate];
  • 注意 :
调用创建方法后,target对象的计数器会加1,直到执行完毕,自动减1。如果是循环执行的话,就必须手动关闭,否则可以不执行释放方法。
3. 特性
  • 存在延迟
不管是一次性的还是周期性的timer的实际触发事件的时间,都会与所加入的RunLoop和RunLoop Mode有关,如果此RunLoop正在执行一个连续性的运算,timer就会被延时出发。重复性的timer遇到这种情况,如果延迟超过了一个周期,则会在延时结束后立刻执行,并按照之前指定的周期继续执行。
  • 必须加入Runloop
使用上面的创建方式,会自动把timer加入MainRunloop的NSDefaultRunLoopMode中。如果使用以下方式创建定时器,就必须手动加入Runloop:
1
2
NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
二、CADisplayLink
1. 创建方法
1
2
3
4
```objc
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
```
2. 停止方法
1
2
3
4
5
```objc
[self.displayLink invalidate];
self.displayLink = nil;
```
**当把CADisplayLink对象add到runloop中后,selector就能被周期性调用,类似于重复的NSTimer被启动了;执行invalidate操作时,CADisplayLink对象就会从runloop中移除,selector调用也随即停止,类似于NSTimer的invalidate方法。**
3. 特性
  • 屏幕刷新时调用
CADisplayLink是一个能让我们以和屏幕刷新率同步的频率将特定的内容画到屏幕上的定时器类。CADisplayLink以特定模式注册到runloop后,每当屏幕显示内容刷新结束的时候,runloop就会向CADisplayLink指定的target发送一次指定的selector消息, CADisplayLink类对应的selector就会被调用一次。所以通常情况下,按照iOS设备屏幕的刷新率60次/秒
  • 延迟
  • iOS设备的屏幕刷新频率是固定的,CADisplayLink在正常情况下会在每次刷新结束都被调用,精确度相当高。但如果调用的方法比较耗时,超过了屏幕刷新周期,就会导致跳过若干次回调调用机会。
  • 如果CPU过于繁忙,无法保证屏幕60次/秒的刷新率,就会导致跳过若干次调用回调方法的机会,跳过次数取决CPU的忙碌程度。
  • 使用场景
从原理上可以看出,CADisplayLink适合做界面的不停重绘,比如视频播放的时候需要不停地获取下一帧用于界面渲染。
4. 重要属性
  • frameInterval
NSInteger类型的值,用来设置间隔多少帧调用一次selector方法,默认值是1,即每帧都调用一次。
  • duration
readOnly的CFTimeInterval值,表示两次屏幕刷新之间的时间间隔。需要注意的是,该属性在target的selector被首次调用以后才会被赋值。selector的调用间隔时间计算方式是:调用间隔时间 = duration × frameInterval。
三、GCD方式
static dispatch_source_t _timer;
NSTimeInterval period = 1.0; //设置时间间隔
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
//在这里执行事件
NSLog(@"Count_______________");
});
dispatch_resume(_timer);

  • 执行一次
1
2
3
4
5
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//执行事件
});
  • 重复执行
1
2
3
4
5
6
7
8
NSTimeInterval period = 1.0; //设置时间间隔
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
//在这里执行事件
});
dispatch_resume(_timer);
GCD的方式,我在网上只能找到这些资料,目前自己还在学习中,以后会更新




iOS:GCD技术——仅仅执行一次和执行多次 dispatch_once和dispatch_apply
只执行一次  (多用于单例模式)
dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
dispatch_once_t *predicate:一个全局的变量      dispatch_block_t block:block函数块
多次执行
dispatch_apply(size_t iterations, dispatch_queue_t queue,void (^block)(size_t));
size_t iterations:执行次数      dispatch_queue_t queue:队列      void (^block)(size_t):block函数块
 
具体举例如下:
1、只执行一次时:
//自定义一个block函数块
//定义blocktypedefvoid (^BLOCK)(void);//将执行代码封装到block中BLOCK myBlock = ^(){staticint count =0; NSLog(@"count=%d",count++);};
//执行dispatch_once函数
//myblock调用//myBlock();//myBlock(); //只会执行一次,GCD oncestatic dispatch_once_t predicate;dispatch_once(&predicate, myBlock);dispatch_once(&predicate, myBlock);
//运行结果:虽然执行两次,只有一个输出
2015-10-0722:54:34.27904-GCD-once[3501:278570] count=0
 
 
2、多次执行时:
//自定义一个block函数
//定义blocktypedefvoid (^BLOCK)(size_t);//将函数封装到blockBLOCK myBlock = ^(size_t size){ staticint count =0; NSLog(@"count=%d",count++); };
//执行dispatch_apply
//GCD多次执行任务dispatch_apply(5, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), myBlock);
//运行结果如下:执行了5次,有5个输出
2015-10-0722:57:59.43805-GCD-apply[3528:280053] count=02015-10-0722:57:59.43805-GCD-apply[3528:280093] count=22015-10-0722:57:59.43805-GCD-apply[3528:280094] count=32015-10-0722:57:59.43805-GCD-apply[3528:280095] count=12015-10-0722:57:59.43905-GCD-apply[3528:280053] count=4
原创粉丝点击