1. 最直接的方法:
[self performSelector:@selector(deleyMethod) withObject:nil afterDelay:1.0];
此方式要求必须在主线程中执行,否则无效。是一种非阻塞的执行方式,暂时未找到取消执行的方法。
缺点:每次要为延时写一个方法。
2. 用 NSTread
[NSThread sleepForTimeInterval:0.06];
3.用GCD
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
});
4.定时器:NSTimer
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
5.使用NSOperationQueue,在应用程序的下一个主循环执行:
[[NSOperationQueue mainQueue] addOperationWithBlock:aBlock];
6.可能是不太好的方法,用animation的completion参数
[UIView animateWithDuration:0.0 delay:5.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
} completion:^(BOOL finished) {
//do stuff here
}];