1、
[self performSelector:@selector(removeAllSceneArrObjects) withObject:nil afterDelay:1.5f];
- (void)removeAllSceneArrObjects {
NSLog(@"\n== 删除所有场景 OK");
}
2、GCD
double delayInSeconds = 2.0; // 延时2秒执行返回
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
});
3、NSTimer
// 是一种非阻塞的执行方式
// 可以通过 NSTimer 类的 - (void)invalidate; 取消执行
dispatch_async(dispatch_get_main_queue(), ^{
// 此方式要求必须在主线程中执行, 否则无效
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(delayMethod)
userInfo:nil
repeats:NO];
});
// ==================================================
@property (nonatomic, strong) NSTimer *timer;
#pragma mark - 定时器控制
- (void)startTimer {
// 创建一个定时器 自动启动
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5
target:self
selector:@selector(nextPage)
userInfo:nil
repeats:YES];
self.timer = [NSTimer timerWithTimeInterval:1.5
target:self
selector:@selector(nextPage)
userInfo:nil
repeats:YES];
// 启动
[self.timer fire];
[[NSRunLoop mainRunLoop] addTimer:self.timer
forMode:NSRunLoopCommonModes];
}
- (void)stopTimer {
[self.timer invalidate];
self.timer = nil; //释放内存
}
网友评论