关于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];//不阻塞线程
 
posted @ 2015-06-24 23:21  jsonxml  阅读(960)  评论(2编辑  收藏  举报