performSelector
[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0f];
此方式要求必须在主线程中执行,否则无效。是一种非阻塞的执行方式
NSTimer
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
用法同performSelector
可以通过NSTimer类的- (void)invalidate;取消执行
sleep
[NSThread sleepForTimeInterval:1.0f]; [self delayMethod];
此方式在主线程和子线程中均可执行,是一种阻塞的执行方式(主线程执行,可能引起UI卡顿)
GCD方式
double delayInSeconds = 1.0;
__block ViewController* bself = self;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[bself delayMethod];
});
此方式在可以在参数中选择执行的线程,是一种非阻塞的执行方式