iOS延时执行的四种方法
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"延时方法的使用->转载:http://blog.csdn.net/chenyong05314/article/details/24695897 ");
[self performSelector:@selector(delayMethodWithPerformSeletor) withObject:nil afterDelay:2.0f];
}
- (void)delayMethodWithPerformSeletor{
NSLog(@"延时方法一:performSeletor");
[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(delayMethodWithNSTimer) userInfo:nil repeats:NO];
}
- (void)delayMethodWithNSTimer{
NSLog(@"延时方法二:定时器:NSTimer");
[NSThread sleepForTimeInterval:3.0f];
[self delayMethodWithSleep];
}
- (void)delayMethodWithSleep{
NSLog(@"延时方法三:sleep方式");
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(), ^{
[bself delayMethodWithGCD];
});
}
- (void)delayMethodWithGCD{
NSLog(@"延时方法四:GCD");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end