ios 中有两个有关Rect的方法CGRectOffset和CGRectInset;
CGRectOffset:
CG_EXTERNCGRect CGRectOffset(CGRect rect,CGFloat dx, CGFloat dy)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
将rect的坐标(origin)按照第二个参数 dx和第三个参数 dy进行平移,rect的大小(size)不变;返回一个新的rect
CGRectInset:
CG_EXTERN CGRect CGRectInset(CGRect rect, CGFloat dx, CGFloat dy) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
通过 第二个参数 dx和第三个参数 dy 重置第一个参数rect 作为结果返回。重置的方式为,首先将rect 的坐标(origin)按照(dx,dy) 进行平移,然后将rect的大小(size.width) 宽度缩小2倍的dx,(size.height)高度缩小2倍的dy;返回一个新的rect。
eg:
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(120, 120, 200, 200)];
[redView setBackgroundColor:[UIColor redColor]];//redView 设置为红色
[self.view addSubview:redView];
//根据redView的大小变换后创建yellowView;
// CGRect yellowRect=CGRectInset(redView.frame, 10, 10);
CGRect yellowRect=CGRectOffset(redView.frame, -10, -10);
UIView *yellowView=[[UIView alloc] initWithFrame:yellowRect];
[yellowView setBackgroundColor:[UIColor yellowColor]];//view2 设置为黄色
[self.view addSubview:yellowView];