1、最简单的调用隐式函数方法

[self performSelector:@selector(deleyMethod) withObject:nil afterDelay:5.0];

必须在主线程中执行,非阻塞线程方式。

2、NSThread

[NSThread sleepForTimeInterval:6.0];

主线程和子线程都可执行,但会阻塞线程,导致UI卡顿。一般用在特殊的阻塞线程执行时使用。

3、GCD

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 5.0 * NSEC_PER_SEC);    

  dispatch_after(popTime, dispatch_get_main_queue(), ^(void){  

     // code to be executed on the main queue after delay    

  }); 

最常用方法之一,可以在参数中选择执行的线程。是一种非阻塞的执行方式。

4、NSTimer

[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(delayMethod) userInfo:nil repeats:NO]; 

必须在主线程中执行。是一种非阻塞的执行方式,通过NSTimer类的- (void)invalidate;取消执行。

 

posted on 2016-11-30 14:09  直到所有灯都熄灭了  阅读(272)  评论(0编辑  收藏  举报