1.在主線程中延遲執(zhí)行某動作捶障,不會卡主主線程,不影響后面的東做執(zhí)行
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
NSLog(@"%@", [NSThread currentThread]);
});
2.在子線程中執(zhí)行某動作,不會卡主整個線程
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
NSLog(@"%@", [NSThread currentThread]);
});
3.實現(xiàn)延遲沈撞,該線程本身在哪個線程中就再哪個線程中執(zhí)行
NSURL *url = [NSURL URLWithString:@"http://59320.jpg.com"];
[self performSelector:@selector(download:) withObject:url afterDelay:3];
4.利用sleep實現(xiàn)延遲(不要用這個励幼,會卡住主線程汰寓,即后面的動作不會執(zhí)行)
[NSThread sleepForTimeInterval:3];
5.NSTimer定時器
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
注:此方法是一種非阻塞的執(zhí)行方式,
取消執(zhí)行方法:- (void)invalidate;即可