坐标系的转换

    xiaoxiao2021-03-25  155

    转换坐标系就是本来中间那个家伙是以左边那个家伙为坐标系的,现在变为以右边那个家伙为坐标系,它的坐标变为多少,就是中间那个控件以右边那个控件为坐标的位置(最后的结果就是两个坐标系的x和y值之间的差别高度和宽度不变)

    // 蓝色

    UIView *blue = [[UIView alloc] init];

    blue.backgroundColor = [UIColor blueColor];

    blue.frame = CGRectMake(0,50, 200,200);

    [self.view addSubview:blue];

    self.blue = blue;

    // 红色

    UIView *red = [[UIView alloc] init];

    red.backgroundColor = [UIColor redColor];

    red.frame = CGRectMake(50,60, 100,100);

    [blue addSubview:red];

    self.red = red;

    // 黄色

    UIView *yellow = [[UIView alloc] init];

    yellow.backgroundColor = [UIColor yellowColor];

    yellow.frame = CGRectMake(10,10, 50,50);

    [red addSubview:yellow];

    self.yellow = yellow;

    // 紫色

    UIView *purple = [[UIView alloc] init];

    purple.backgroundColor = [UIColor purpleColor];

    purple.frame = CGRectMake(150,350, 100,100);

    [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.yellowself.blue中的位置和尺寸

        //    CGRect newRect = [self.yellow convertRect:self.yellow.bounds toView:self.blue];{60,70,50,50}

        // 计算self.yellowself.purple中的位置和尺寸

        //    CGRect newRect = [self.yellow.superview convertRect:self.yellow.frame toView:self.purple];{60,70,50,50}

        

        // 计算self.redself.yellow中的位置和尺寸

        //    CGRect newRect = [self.red convertRect:self.red.bounds toView:self.yellow];

        // 计算self.redself.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));

    }

    转载请注明原文地址: https://ju.6miu.com/read-19643.html

    最新回复(0)