XZ_iOS之转换坐标系

来源:互联网 发布:mac下载特别慢 编辑:程序博客网 时间:2024/05/16 10:47
默认情况下,frame是以父控件左上角为坐标原点;
转换坐标系:可以转换坐标系原点,改变frame的参照点

// 蓝色

UIView *blue = [[UIViewalloc]init];

blue.backgroundColor = [UIColorblueColor];

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

[self.viewaddSubview:blue];

// 红色

UIView *red = [[UIViewalloc]init];

red.backgroundColor = [UIColorredColor];

red.frame = CGRectMake(100,150,100,100);

[self.viewaddSubview:red];

// 计算self.red在其他控件中的位置和尺寸

CGRect newRect = [red convertRect:CGRectMake(50,50,50,50) toView:blue];

// 以字符串的形式打印转换完的坐标

NSLog(@"%@",NSStringFromCGRect(newRect));

结果打印:



CGRectnewFrame = [from convertRect:from.boundstoView:window];
等价于 
CGRectnewFrame = [from.superview convertRect:from.frame toView:window];
等价于:toView写成nil(空)就是window
CGRectnewFrame = [fromconvertRect:from.boundstoView:window];
self.containerView.y=CGRectGetMaxY(newFrame);
self.containerView.centerX=CGRectGetMidX(newFrame);
原理:左边的from和右边的windows相差多少,from的bounds就加上多少
0 0
原创粉丝点击