1.frame
a》什么是frame
类型:CGRect结构体
作用:用于描述视图左顶点在父视图的位置,及视图在父视图中占据的大小
b》什么时候使用frame
当将一个视图添加到一个父视图下做子视图的时候,需要设置frame定位
c》直接修改frame,其他属性是否受影响?
- (void)viewDidLoad {
[super viewDidLoad];
self.iv=[[UIImageView alloc]init];
self.iv.frame=CGRectMake(100, 100, 100, 100);
[self showInfo];
[self changeFrame];
-(void)showInfo
{
NSLog(@"\nframe:%@\nbounds:%@\ncenter:%@\ntransform:%@",NSStringFromCGRect(self.iv.frame),NSStringFromCGRect(self.iv.bounds),NSStringFromCGPoint(self.iv.center),NSStringFromCGAffineTransform(self.iv.transform));
}
-(void)changeFrame
{
self.iv.frame=CGRectMake(50, 50, 50, 50);
NSLog(@"\n改变frame\nframe:%@\nbounds:%@\ncenter:%@\ntransform:%@",NSStringFromCGRect(self.iv.frame),NSStringFromCGRect(self.iv.bounds),NSStringFromCGPoint(self.iv.center),NSStringFromCGAffineTransform(self.iv.transform));
}
2016-01-24 11:11:01.953
frame:{{100, 100}, {100, 100}}
bounds:{{0, 0}, {100, 100}}
center:{150, 150}
transform:[1, 0, 0, 1, 0, 0]
2016-01-24 11:11:01.954
改变frame
frame:{{50, 50}, {50, 50}}
bounds:{{0, 0}, {50, 50}}
center:{75, 75}
transform:[1, 0, 0, 1, 0, 0]
当改变frame属性时,
bounds:YES
center:YES
transform:NO
2.bounds
a》什么是bounds
类型:CGRect结构体类型
作用:记录自身坐标系的基准值及自身的大小
b》什么时候使用bounds
当要读取视图的实际大小时,用bounds,
也可以改变自身坐标系的基准点来调准子视图的位置
补充:视图自身的坐标系基准值默认是(0,0),w和h默认和frame中的h,w相等,当想读取视图大小时读取自己的bounds
c》直接修改boun,其他属性是否受影响?
- (void)viewDidLoad {
[super viewDidLoad];
self.iv=[[UIImageView alloc]init];
self.iv.frame=CGRectMake(100, 100, 100, 100);
[self showInfo];
//[self changeFrame];
[self changeBounds];
// [self changeCenter];
// [self changeTransform];
}
-(void)showInfo
{
NSLog(@"\nframe:%@\nbounds:%@\ncenter:%@\ntransform:%@",NSStringFromCGRect(self.iv.frame),NSStringFromCGRect(self.iv.bounds),NSStringFromCGPoint(self.iv.center),NSStringFromCGAffineTransform(self.iv.transform));
}
-(void)changeBounds
{
self.iv.bounds=CGRectMake(0,0,50,50);
NSLog(@"\n改变bounds\nframe:%@\nbounds:%@\ncenter:%@\ntransform:%@",NSStringFromCGRect(self.iv.frame),NSStringFromCGRect(self.iv.bounds),NSStringFromCGPoint(self.iv.center),NSStringFromCGAffineTransform(self.iv.transform));
}
2016-01-24 11:26:36.350
frame:{{100, 100}, {100, 100}}
bounds:{{0, 0}, {100, 100}}
center:{150, 150}
transform:[1, 0, 0, 1, 0, 0]
2016-01-24 11:26:36.351
改变bounds
frame:{{125, 125}, {50, 50}}
bounds:{{0, 0}, {50, 50}}
center:{150, 150}
transform:[1, 0, 0, 1, 0, 0]
改变bounds时:
frame YES
center NO
transform NO
3frame
a》什么是center
类型:CGPoint结构体类型
作用:记录视图中心点在父视图上的位置
b》什么时候使用center
当需要改变视图在父视图上的位置时,可以通过改变center来实现
c》直接修改center,其他属性是否受影响?
- (void)viewDidLoad {
[super viewDidLoad];
self.iv=[[UIImageView alloc]init];
self.iv.frame=CGRectMake(100, 100, 100, 100);
[self showInfo];
//[self changeFrame];
//[self changeBounds];
[self changeCenter];
// [self changeTransform];
}
-(void)showInfo
{
NSLog(@"\nframe:%@\nbounds:%@\ncenter:%@\ntransform:%@",NSStringFromCGRect(self.iv.frame),NSStringFromCGRect(self.iv.bounds),NSStringFromCGPoint(self.iv.center),NSStringFromCGAffineTransform(self.iv.transform));
}
-(void)changeCenter
{
self.iv.center=CGPointMake(100, 100);
NSLog(@"\n改变center\nframe:%@\nbounds:%@\ncenter:%@\ntransform:%@",NSStringFromCGRect(self.iv.frame),NSStringFromCGRect(self.iv.bounds),NSStringFromCGPoint(self.iv.center),NSStringFromCGAffineTransform(self.iv.transform));
}
2016-01-24 11:34:55.866
frame:{{100, 100}, {100, 100}}
bounds:{{0, 0}, {100, 100}}
center:{150, 150}
transform:[1, 0, 0, 1, 0, 0]
2016-01-24 11:34:55.867
改变center
frame:{{50, 50}, {100, 100}}
bounds:{{0, 0}, {100, 100}}
center:{100, 100}
transform:[1, 0, 0, 1, 0, 0]<span style="font-family: 'Heiti SC Light';"> </span>
改变center时:
frame YES
bounds NO
transform NO
4.transform
a》什么是transform
类型:CGAffineTransform
作用:变形
b》什么时候使用transform
需要发生旋转,位移,缩放效果时
c》直接修改transform,其他属性是否受影响?
- (void)viewDidLoad {
[super viewDidLoad];
self.iv=[[UIImageView alloc]init];
self.iv.frame=CGRectMake(100, 100, 100, 100);
[self showInfo];
//[self changeFrame];
//[self changeBounds];
//[self changeCenter];
[self changeTransform];
}
-(void)showInfo
{
NSLog(@"\nframe:%@\nbounds:%@\ncenter:%@\ntransform:%@",NSStringFromCGRect(self.iv.frame),NSStringFromCGRect(self.iv.bounds),NSStringFromCGPoint(self.iv.center),NSStringFromCGAffineTransform(self.iv.transform));
}
-(void)changeTransform
{
self.iv.transform=CGAffineTransformRotate(self.iv.transform, M_PI_4);
NSLog(@"\n改变transform\nframe:%@\nbounds:%@\ncenter:%@\ntransform:%@",NSStringFromCGRect(self.iv.frame),NSStringFromCGRect(self.iv.bounds),NSStringFromCGPoint(self.iv.center),NSStringFromCGAffineTransform(self.iv.transform));
}
2016-01-24 11:41:36.062
frame:{{100, 100}, {100, 100}}
bounds:{{0, 0}, {100, 100}}
center:{150, 150}
transform:[1, 0, 0, 1, 0, 0]
2016-01-24 11:41:36.062
改变transform
frame:{{79.289321881345259, 79.289321881345245}, {141.42135623730951, 141.42135623730951}}
bounds:{{0, 0}, {100, 100}}
center:{150, 150}
transform:[0.70710678118654757, 0.70710678118654746, -0.70710678118654746, 0.70710678118654757, 0, 0]
改变transform时:
frame YES
bounds NO
center NO
总结:视图的数据分为两个空间,一个是用户看的到的用户空间,一个是用户看不到的设备空间,
bounds和center记录视图在设备空间上实际的位置和大小,transform为视图从设备空间映射到用户空间的映射规则,
frame记录了视图显示在用户空间上的位置及大小。
本人是一枚初学ios的菜鸟,欢迎志同道合的朋友加我的微博
by无尽的丛林
微博:拉轰的笨小宁