#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self delay];
}
- (void)delay {
NSLog(@"--delay-");
//延迟执行
//[self performSelector:@selector(run) withObject:nil afterDelay:2.0];
//[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
//GCD中的延迟执行
/* 参数说明
*
* 第一个参数:设置时间(GCD中的时间单位是纳秒)
* 第二个参数:队列(决定block中的任务在哪个线程中执行,如果是主队列就是主线程,否在就在子线程)
* 第三个参数:设置任务
* 原理:(哪个简单)
* A 先把任务提交到队列,然后等两秒再执行 错误
* B 先等两秒,再把任务提交到队列 正确
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
NSLog(@"-----GCD------%@",[NSThread currentThread]);
});
}
- (void)run {
NSLog(@"run--");
}
@end