一 Frame
1.定义:用来表示 View 的位置和大小 原点 以父视图 坐标系为参考
2. 赋值: Frame 不能单独赋值
redView .frame.size.width =10 //error
view 的 frame 只能整体赋值
redView .frame = CGRectMake(60, 184, 100, 200);
二 center 中心点
1.视图的中心点在父视图的坐标系的位置
公式:((x+w/2),(y+h/2));
NSLog(@"%@",NSStringFromCGPoint(redView.center));
三 bounds
1. 视图在自身坐标中的位置 默认自身坐标系的原点 在视图的左上角
2.由 bounds 的位置 确定自身 原点的位置
① x>0 y>0;左上
② x>0 y< 0; 左下
③ x<0 y<0;右上
④ x < 0 y >0 ;右下
// center frame bounds 相互的影响
/*
1.frame 修改 会影响 center 和 bounds
2.center修改 会影响 frame,bounds不变
3.bounds修改 会影响 frame,center 不变
*/
四 层级关系
创建 View
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
view1 .backgroundColor = [UIColor purpleColor];
[self.window addSubview:view1];
[view1 release];
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
view2 .backgroundColor = [UIColor redColor];
[self.window addSubview:view2];
[view2 release];
// 用父视图 把子视图提前
[self.window bringSubviewToFront:view1];
// 用父视图 把子视图放在最后
[self.window sendSubviewToBack:view1];
// 用父视图 把子视图交换位置 用下标
[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
2.如果要插入一个新的View
UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(70, 70, 200, 200)];
view3.backgroundColor = [UIColor yellowColor];
// 插入到 view1 的下面
[self.window insertSubview:view3 belowSubview:view1];
// 插入指定位置
[self.window insertSubview:view3 atIndex:1];
// 插入最上面 view2 的上面
[self.window insertSubview:view3 aboveSubview:view2];
[view3 release];
// 删除
// 1.自我删除
[view1 removeFromSuperview];
//随机赋色
#define COLORVALUE arc4random()%255/256.0;
UIColor *color = [UIColor colorWithRed:COLORVALUE green:COLORVALUE blue:COLORVALUE alpha:1.0];
view3 .backgroundColor = color;
frame和bounds是UIView中的两个属性(property)。
frame指的是:该view在父view坐标系统中的位置和大小。(参照点是父亲的坐标系统)
bounds指的是:该view在本身坐标系统中 的位置和大小。(参照点是本身坐标系统)
center指的是:该view的中心点在父view坐标系统中的位置和大小。(参照点是父亲的坐标系统)
翻译文档上的 bounds是指这个view在它自己坐标系的坐标和大小 而frame指的是这个view在它superview的坐标系的坐标和大小 区别主要在坐标系这一块。 很明显一个是自己为原点的坐标系,一个是以屏幕为原点的坐标系。