iOS开发 ☞ 各种CG结构体

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的位置不能传入子视图的坐标,需要注意一下。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值