1 //
2 // YYViewController.m
3 // 01-GCD的常见使用(延迟执行)
4 //
5 // Created by apple on 14-6-25.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10
11 @interface YYViewController ()
12
13 @end
14
15 @implementation YYViewController
16
17 - (void)viewDidLoad
18 {
19 [super viewDidLoad];
20 NSLog(@"打印线程----%@",[NSThread currentThread]);
21 //延迟执行
22 //第一种方法:延迟3秒钟调用run函数
23 [self performSelector:@selector(run) withObject:nil afterDelay:2.0];
24
25 }
26 -(void)run
27 {
28 NSLog(@"延迟执行----%@",[NSThread currentThread]);
29 }
30
31 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
32 {
33 //在异步函数中执行
34 dispatch_queue_t queue = dispatch_queue_create("wendingding", 0);
35
36 dispatch_sync(queue, ^{
37 [self performSelector:@selector(test) withObject:nil afterDelay:1.0];
38 });
39 NSLog(@"异步函数");
40 }
41 -(void)test
42 {
43 NSLog(@"异步函数中延迟执行----%@",[NSThread currentThread]);
44 }
45 @end