1、CGRectInset
CGRect CGRectOffset(CGRect rect, CGFloat dx, CGFloat dy)是以rect为中心,根据dx和dy来实现缩小。
UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
UIView * view2 = [UIView new];
view2.frame = CGRectInset(view1.frame, 30, 30);
view2.backgroundColor = [UIColor blackColor];
[self.view addSubview:view2];
如图所示,可以理解为,高度和宽度相对于view1分别缩小20像素。正值表示缩小,负值表示扩大。
2.CGRectOffset
CGRect CGRectOffset(CGRect rect, CGFloat dx, CGFloat dy)
它是以rect左上角为基点,向X轴和Y轴偏移dx和dy像素。
UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
UIView * view2 = [UIView new];
view2.frame = CGRectOffset(view1.frame, 20, 20);
view2.backgroundColor = [UIColor blackColor];
[self.view addSubview:view2];
由图可以看出,黑色以红色的左上角为基准点,向x轴和y轴分别偏移了20像素。