1、CGRectInset(<#CGRect rect#>, <#CGFloat dx#>, <#CGFloat dy#>)
中心相同
CG_EXTERN CGRect CGRectInset(CGRect rect, CGFloat dx, CGFloat dy)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor orangeColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectInset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
[self.view addSubview:otherView];
2、CGRectOffset(<#CGRect rect#>, <#CGFloat dx#>, <#CGFloat dy#>)
位置不同
CG_EXTERN CGRect CGRectOffset(CGRect rect, CGFloat dx, CGFloat dy)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor purpleColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectOffset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
[self.view addSubview:otherView];
3、CGRectGetMidY(<#CGRect rect#>) 、 CGRectGetMidX(<#CGRect rect#>)
获取中心点坐标
CG_EXTERN CGFloat CGRectGetMidX(CGRect rect)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
CG_EXTERN CGFloat CGRectGetMidY(CGRect rect)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor purpleColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectOffset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
CGFloat centerX = CGRectGetMidX(view.frame);
CGFloat centerY = CGRectGetMidX(view.frame);
NSLog(@"%f",centerX);
NSLog(@"%f",centerY);
[self.view addSubview:view];
[self.view addSubview:otherView];
4、CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>)
是否相交
In English,intersect means 交集
CG_EXTERN bool CGRectIntersectsRect(CGRect rect1, CGRect rect2)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor purpleColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectOffset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
[self.view addSubview:otherView];
BOOL isIntersect = CGRectIntersectsRect(view.frame, otherView.frame);
NSLog(@"%d",isIntersect);
5、CGRectIntersection(<#CGRect r1#>, <#CGRect r2#>)
相交区域
CG_EXTERN CGRect CGRectIntersection(CGRect r1, CGRect r2)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor purpleColor];
UIView *otherView = [[UIView alloc] init];
otherView.frame = CGRectOffset(view.frame, 2, 4);
otherView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
[self.view addSubview:otherView];
CGRect rect = CGRectIntersection(view.frame, otherView.frame);
NSLog(@"%@",NSStringFromCGRect(rect));
UIView *intersectView = [[UIView alloc] init];
intersectView.frame = rect;
intersectView.backgroundColor = [UIColor redColor];
[self.view addSubview:intersectView];
6、CGRectIsNull(<#CGRect rect#>)
是否为空
CG_EXTERN bool CGRectIsNull(CGRect rect)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
UIView *view1 = [[UIView alloc] init];
view1.frame = CGRectMake(100, 150, 100, 100);
view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view1];
CGRect intersectRect = CGRectIntersection(view.frame, view1.frame);
//0说明有交集
NSLog(@"%d",CGRectIsNull(intersectRect));
7、CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
判断两个矩形是否相同,包括位置和形状
CG_EXTERN bool CGRectEqualToRect(CGRect rect1, CGRect rect2)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
2016年12月21日更新
iOS中的坐标转换:
经常遇到的需求是把cell上的子视图的坐标转为窗口的坐标,我们需要的转换如下:
[cell convertPoint:tmpImage.center toView:destinationView] 这里的cell的位置不能传入子视图的坐标,需要注意一下。