iOS ->几秒后 执行某个方法
2016-11-02 本文已影响188人
侠永
在开发过程中经常会用到:几秒后 执行某个方法
1、NSRunLoop
[self performSelector:@selector(laterExecute) withObject:nil afterDelay:5.0f];
2、定时器
[NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(laterExecute) userInfo:nil repeats:NO];
3、GCD
double delayInSeconds = 5.0;
__block ViewController *selfBlcok = self;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
[selfBlcok laterExecute];
});