计时器延迟 NSTimer和CADisplaylink GCD中的延迟
1,NStimer時間間隔比較大,大于1秒;
CADisplayLink 時間間隔比較小,0.01秒;
2,創建啟動計時器:
[NSTimer scheduledTimeInterval:0.5 target:self selector:@select(nextImage) userInfo:nil repeat:Yes];
3,停止計時器
調用NSTimer對象的invalidate方法。一旦停止就不能再用了,只能重新創建一個新的計時器;
[self.timer invalidate];
self.timer = nil;//計時器已經廢了,指向nil;
4,nstimer 優先級比較低,有同界面的其他空間接收交行的時候,NSTimer會停止,所以我們要設置優先級;
修改優先級與其他控件相同:
1.獲取當前消息循環對象:
NSRunloop *runloop = [NSRunLoop currentRunLoop];
改變self.timer對象的優先級
runloop addTimer:self.timer forMode:NSRunLoopCommonModes];
?
GCD中的延遲:dispatch_after snippet - GCD:Dispatch After方法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0 * NSEC_PER_SEC)),dispatch_get_main_queue(),^{
延遲后要實現的方法。
});
關于OC中的幾種代碼延遲執行方式 第一種:
[UIView animateWithDuration:3 delay:3 options:1 animations:^{
? ? ? ? self.btn.transform = CGAffineTransformMakeTranslation(300, 400);
? ? } completion:^(BOOL finished) {
? ? ? ? NSLog(@"view animation結束");
? ? }];//不會阻塞線程,animations ?block中的代碼對于是支持animation的代碼,才會有延時效果,
? ? ? ? ?對于不支持animation的代碼 則 不會有延時效果
第二種:
[NSThread sleepForTimeInterval:3];//阻塞線程,浪費性能 ,一般不推薦用
第三種:最常用
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ?
? ? });//定制了延時執行的任務,不會阻塞線程,效率較高(推薦使用)
??
第四種:
[self performSelector:@selector(test) withObject:nil afterDelay:3];//不阻塞線程
轉載于:https://www.cnblogs.com/yangqinglong/p/5534201.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的计时器延迟 NSTimer和CADisplaylink GCD中的延迟的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浪潮之巅感触
- 下一篇: input 模糊搜索