码迷,mamicode.com
首页 > 移动开发 > 详细

IOS开发定时器延时的探究

时间:2016-10-19 20:20:57      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

在日常开发中,我们经常要延时等待(如网络请求,UI更新完)然后做一些事情,或者是做一些周期性的处理。这个时候就要求我们实现一个周期性的定时器进行延时操作。常用的延时实现方法有下面三种:

1.NStimer实现

不管是一次性的还是周期性的timer的实际触发事件的时间,都会与所加入的RunLoop和RunLoop Mode有关,如果此RunLoop正在执行一个连续性的运算,timer就会被延时触发。重复性的timer遇到这种情况,如果延迟超过了一个周期,则会在延时结束后立刻执行,并按照之前指定的周期继续执行。(PS: 所以有时使用timer,会偶尔出现连续两次刷新的情况,可能上一次timer被阻塞)

NSTimer *timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeUI) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

上面方式:每隔一秒调用一次定时器的block

使用注意:1. 这个runLoop运行模式,建议使用NSRunLoopCommonModes;使用NSDefaultRunLoopMode,在屏幕上有UI更新(如ScrollView滑动时),runLoop就会被切换到其他的模式(NSEventTrackingRunLoopMode),此时timer不会被执行。NSRunLoopCommonModes中包含了NSEventTrackingRunLoopMode,timer照样被执行

2.当不在使用这个timer时,需要手动调用invalidate方法将timer释放并重置为nil。因为在创建timer时,会默认对target进行一次retain,如果不手动释放timer,target对象也得不到释放会造成内存泄露

3.主线程开启,回调的selector默认也在主线程执行。如果想要多线程执行timer,需要手动开启一个NSthread,值得主要的是:由于子线程的runLoop默认是关闭的,所以需要手动开启子线程的runLoop,否则timer永远不会被执行

//创建并执行新的线程

NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(newThread) object:nil];

[thread start];

//在新线程开启定时器

- (void)openTimer

@autoreleasepool

{

//在当前Run Loop中添加timer,模式是默认的NSDefaultRunLoopMode

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(timer_callback) userInfo:nil repeats:YES];

//开始执行新线程的Run Loop

[[NSRunLoop currentRunLoop] run];

}

}

2.CADisplayLink:

frameInterval :NSInteger类型的值,用来设置间隔多少帧调用一次selector方法,默认值是1,即每帧都调用一次。

duration:readOnly的CFTimeInterval值,表示两次屏幕刷新之间的时间间隔。需要注意的是,该属性在target的selector被首次调用以后才会被赋值。selector的调用间隔时间计算方式是:调用间隔时间 = duration * frameInterval (调用一次方法刷新帧数 *刷新每一帧所需的时间)

CADisplayLink的使用跟timer比较类似,这里就不详细介绍。朋友们可以参考参考下面这个链接。

http://blog.csdn.net/ch_soft/article/details/9408855

3.GCD实现Timer

GCD中的Timer应该是最灵活的,而且是多线程的。GCD中的Timer是靠Dispatch Source来实现的。

最近使用GCD的timer做一个验证码发送的例子。整个使用过程分为五步,代码如下:

__block NSInteger time = 59; //倒计时时间

//1.创建一个全局并发队列

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//2.创建一个timer加到队列中

dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

//3.设置timer的执行周期,最后一个参数表示延时秒数为0,尽可能精确

dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);

//4.设置timer周期执行的block

dispatch_source_set_event_handler(_timer, ^{

if(time <= 0){ //倒计时结束,关闭定时器,还原UI

dispatch_source_cancel(_timer);

dispatch_async(dispatch_get_main_queue(), ^{

//设置按钮的样式

[self.getMsgBtn setTitle:@"获取验证码" forState:UIControlStateNormal];

[self.getMsgBtn setTitleColor:ZGColor(25, 111, 255) forState:UIControlStateNormal];

[self.getMsgBtn setBackgroundColor:[UIColor whiteColor]];

self.getMsgBtn.titleLabel.font = [UIFont systemFontOfSize: 14.0];

self.getMsgBtn.userInteractionEnabled = YES;

});

}else{ //否则继续计时

int leftSeconds = time % 60;

dispatch_async(dispatch_get_main_queue(), ^{

//设置按钮显示读秒效果

[self.getMsgBtn setTitle:[NSString stringWithFormat:@"%d秒后重新发送",leftSeconds] forState:UIControlStateNormal];

[self.getMsgBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[self.getMsgBtn setBackgroundColor:[UIColor lightGrayColor]];

self.getMsgBtn.titleLabel.font = [UIFont systemFontOfSize: 13.0];

self.getMsgBtn.userInteractionEnabled = NO;

});

time--;

}

});

//5.启动timer

dispatch_resume(_timer);

使用注意:由于dispatch_source_t 创建后默认是暂停状态,需要手动开启才会执行。

另外,实际开发中有需求:等待reload执行完刷新完tableView,然后做一些操作

想要程序延迟到reloadData结束在操作,可以用以下方法

方法1:

[self.tableView reloadData];

[self.tableView layoutIfNeeded];

//刷新完成

…….

layoutIfNeeded会强制重绘并等待完成。

方法2:

[objc] view plain copy

[self.tableView reloadData];

dispatch_sync(dispatch_get_main_queue(), ^{

//刷新完成

});

reloadData会在主队列执行,而dispatch_get_main_queue会等待机会,直到主队列空闲才执行。

IOS开发定时器延时的探究

标签:

原文地址:http://www.cnblogs.com/coderMrZhou/p/5978289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!