iOS开发---转换坐标系

时间:2023-03-08 17:33:12
 - (void)viewDidLoad
{
[super viewDidLoad]; // 蓝色
UIView *blue = [[UIView alloc] init];
blue.backgroundColor = [UIColor blueColor];
blue.frame = CGRectMake(, , , );
[self.view addSubview:blue];
self.blue = blue; // 红色
UIView *red = [[UIView alloc] init];
red.backgroundColor = [UIColor redColor];
red.frame = CGRectMake(, , , );
[blue addSubview:red];
self.red = red; // 黄色
UIView *yellow = [[UIView alloc] init];
yellow.backgroundColor = [UIColor yellowColor];
yellow.frame = CGRectMake(, , , );
[red addSubview:yellow];
self.yellow = yellow; // 紫色
UIView *purple = [[UIView alloc] init];
purple.backgroundColor = [UIColor purpleColor];
purple.frame = CGRectMake(, , , );
[self.view addSubview:purple];
self.purple = purple;
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"%@", NSStringFromCGRect(self.yellow.bounds));
// NSLog(@"%@", NSStringFromCGRect(self.yellow.frame)); // 计算self.yellow在self.blue中的位置和尺寸
// CGRect newRect = [self.yellow convertRect:self.yellow.bounds toView:self.blue]; // 计算self.yellow在self.purple中的位置和尺寸
// CGRect newRect = [self.yellow.superview convertRect:self.yellow.frame toView:self.purple]; // 计算self.red在self.yellow中的位置和尺寸
// CGRect newRect = [self.red convertRect:self.red.bounds toView:self.yellow];
// 计算self.red在self.yellow中的位置和尺寸
// CGRect newRect = [self.yellow convertRect:self.red.bounds fromView:self.red]; // 计算self.red在屏幕中的位置和尺寸(nil代表屏幕)
CGRect newRect = [self.red convertRect:self.red.bounds toView:nil];
NSLog(@"%@", NSStringFromCGRect(newRect));
}