第一种:
[UIView animateWithDuration:1 delay:2 options:1 animations:^{
} completion:^(BOOL finished) {
}];
//不会阻塞线程,animations block中的代码对于是支持animation的代码,才会有延时效果,对于不支持animation的代码 则 不会有延时效果。
第二种:
[NSThread sleepForTimeInterval:3];//阻塞线程,浪费性能 ,一般不推荐用
第三种:最常用,延迟2秒执行:
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){
// code to be executed on the main queue after delay
});
//定制了延时执行的任务,不会阻塞线程,效率较高(推荐使用)
第四种:最直接的方法,这种方法的缺点:每次要为延时写一个方法
[self performSelector:@selector(test) withObject:nil afterDelay:3];//不阻塞线程
5.使用NSOperationQueue,在应用程序的下一个主循环执行:
[[NSOperationQueue mainQueue] addOperationWithBlock:aBlock];这个和调用performSelector: with afterDelay of 0.0f等价