iOS中常用的几种延时加载/执行的处理办法

在开发过程中

我们会常常需要用到这样的延迟处理这种技术

那么,

一般你们会怎么去做这样的一个延时操作呢?

比如,

用户登录成功以后,提示登录成功,然后再将控制器从登录页切到主页

又或是,等待一个动画完成以后移除不必要的UI控件

 

 

今天 就说说iOS开发中常用的几种延时操作的方法:

1.performSelector

[self performSelector:@selector(removeViews) withObject:nil afterDelay:5.0];


这个好理解,延时调用

 

2.NSTimer 触发定时任务

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeViews) userInfo:nil repeats:NO];
[timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:5.0]];


3.使用GCD

dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
    [self removeViews];
});


4.NSThread  阻塞子线程

 [NSThread sleepForTimeInterval:5];
 [self removeViews];


怎么样,挺好用的吧!!!!!

 

写完该睡觉了   。。。。

 

 

你可能感兴趣的:(iOS精华)