1、NSTimer(不阻塞线程)
//创建一个定时器
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doSomething) userInfo:nil repeats:NO];
//延迟要做的事
- (void)doSomething {
//做你想做的事
}
//取消定时器
[timer invalidate];
timer = nil;
2、performSelector…withObject…afterDelay(不阻塞线程)
//执行
[self performSelector:@selector(doSomething) withObject:@"cancel" afterDelay:1];
- (void)doSomething {
//做你想做的事
}
//取消特定的某个延迟操作,selector和object要与执行相对应
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(doSomething) object:@"cancel"];
//取消performSelector的所有延迟操作
[NSObject cancelPreviousPerformRequestsWithTarget:self];
3、GCD延迟方法(不阻塞线程)
//执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//做你想做的事
});
4、NSThread线程的sleep(阻塞线程)
[NSThread sleepForTimeInterval:1.0];








网友评论