iOS开发---转换坐标系

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     
 5     // 蓝色
 6     UIView *blue = [[UIView alloc] init];
 7     blue.backgroundColor = [UIColor blueColor];
 8     blue.frame = CGRectMake(0, 50, 200, 200);
 9     [self.view addSubview:blue];
10     self.blue = blue;
11     
12     // 红色
13     UIView *red = [[UIView alloc] init];
14     red.backgroundColor = [UIColor redColor];
15     red.frame = CGRectMake(50, 60, 100, 100);
16     [blue addSubview:red];
17     self.red = red;
18     
19     // 黄色
20     UIView *yellow = [[UIView alloc] init];
21     yellow.backgroundColor = [UIColor yellowColor];
22     yellow.frame = CGRectMake(10, 10, 50, 50);
23     [red addSubview:yellow];
24     self.yellow = yellow;
25     
26     // 紫色
27     UIView *purple = [[UIView alloc] init];
28     purple.backgroundColor = [UIColor purpleColor];
29     purple.frame = CGRectMake(150, 350, 100, 100);
30     [self.view addSubview:purple];
31     self.purple = purple;
32 }
33 
34 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
35 {
36 //    NSLog(@"%@", NSStringFromCGRect(self.yellow.bounds));
37 //    NSLog(@"%@", NSStringFromCGRect(self.yellow.frame));
38     
39     // 计算self.yellow在self.blue中的位置和尺寸
40 //    CGRect newRect = [self.yellow convertRect:self.yellow.bounds toView:self.blue];
41     
42     
43     // 计算self.yellow在self.purple中的位置和尺寸
44 //    CGRect newRect = [self.yellow.superview convertRect:self.yellow.frame toView:self.purple];
45     
46     // 计算self.red在self.yellow中的位置和尺寸
47     //    CGRect newRect = [self.red convertRect:self.red.bounds toView:self.yellow];
48     // 计算self.red在self.yellow中的位置和尺寸
49 //    CGRect newRect = [self.yellow convertRect:self.red.bounds fromView:self.red];
50     
51     // 计算self.red在屏幕中的位置和尺寸(nil代表屏幕)
52     CGRect newRect = [self.red convertRect:self.red.bounds toView:nil];
53     NSLog(@"%@", NSStringFromCGRect(newRect));
54 }

 

posted @ 2015-07-31 10:49  TianXiang.Liu  阅读(320)  评论(0编辑  收藏  举报