CGRectUnion
CGRectUnion接受两个CGRect结构体作为参数并且返回一个能够包含这两个矩形的最小矩形。听起来可能没什么,我相信你也可以用几行代码轻松实现这个功能,不过 CGGeometry 做的是给你提供一些方法让你的代码更干净、可读性更强。

如果你把下面代码片段加到一个 view controller 的viewDidLoad方法中,你将在模拟器中看到如下结果。那个灰色的矩形就是使用CGRectUnion的结果。

// CGRectUnion
CGRect frame1 = CGRectMake(80.0, 100.0, 150.0, 240.0);
CGRect frame2 = CGRectMake(140.0, 240.0, 120.0, 120.0);
CGRect frame3 = CGRectUnion(frame1, frame2); UIView *view1 = [[UIView alloc] initWithFrame:frame1];
[view1 setBackgroundColor:[UIColor redColor]]; UIView *view2 = [[UIView alloc] initWithFrame:frame2];
[view2 setBackgroundColor:[UIColor orangeColor]]; UIView *view3 = [[UIView alloc] initWithFrame:frame3];
[view3 setBackgroundColor:[UIColor grayColor]]; [self.view addSubview:view3];
[self.view addSubview:view2];
[self.view addSubview:view1];

CGRectDivide

另一个有用的方法是CGRectDivide,它帮你把一个给定矩形分割成两个。看看下面的代码和截图来了解它是怎么运作的。

// CGRectDivide
CGRect frame = CGRectMake(10.0, 50.0, 300.0, 300.0);
CGRect part1;
CGRect part2;
CGRectDivide(frame, &part1, &part2, 100.0, CGRectMaxYEdge); UIView *view1 = [[UIView alloc] initWithFrame:frame];
[view1 setBackgroundColor:[UIColor grayColor]]; UIView *view2 = [[UIView alloc] initWithFrame:part1];
[view2 setBackgroundColor:[UIColor orangeColor]]; UIView *view3 = [[UIView alloc] initWithFrame:part2];
[view3 setBackgroundColor:[UIColor redColor]]; [self.view addSubview:view1];
[self.view addSubview:view2];
[self.view addSubview:view3];

如果你不使用CGRectDivide来计算红色和橙色矩形的话,你可能要多谢几十行代码。不信你就试试。

比较和包含

用下面六个方法来比较几何结构和检查包含关系非常简单。

  • CGPointEqualToPoint

  • CGSizeEqualToSize

  • CGRectEqualToRect

  • CGRectIntersectsRect

  • CGRectContainsPoint

  • CGRectContainsRect

CGGeometry Reference 还有一些其他宝贝,比如CGPointCreateDictionaryRepresentation可以用来将一个 CGPoint 结构体转换为一个 CGDictionaryRefCGRectIsEmpty可以用来检查一个矩形的宽高是否都为零。更多详情请看[《CGGeometry Reference 文档》]()

来源:https://segmentfault.com/a/1190000004695617?hmsr=toutiao.io

CGGeometry Reference的更多相关文章

  1. CGGeometry Reference (一)

    知识点 frame 与bounds 的区别 1.frame 是这个视图的大小在父视图的位置 . 如x 20 y 20  width 200 height 300 2.bounds 是这个视图的大小在自 ...

  2. iOS 基础:Frames、Bounds 和 CGGeometry

    https://segmentfault.com/a/1190000004695617 原文:<iOS Fundamentals: Frames, Bounds, and CGGeometry& ...

  3. iOS编码规范

      The official raywenderlich.com Objective-C style guide.   This style guide outlines the coding con ...

  4. IOS开发-代码规范

    代码风格的重要性对于一个团队和项目来说不言而喻.网上有许多 Objective-C 的代码风格,但这份简洁而又最符合苹果的规范,同时有助于养成良好的代码习惯,也是我们团队一直遵循的代码风格. 写法没有 ...

  5. 官方的objective - c风格指南。

    The official raywenderlich.com Objective-C style guide. This style guide outlines the coding convent ...

  6. Objective-C 编码风格指南

    本文转自:[Objective-C 编码风格指南 | www.samirchen.com][2] ## 背景 保证自己的代码遵循团队统一的编码规范是一个码农的基本节操,能够进入一个有统一编码规范的团队 ...

  7. [转]IOS开发中的CGFloat、CGPoint、CGSize和CGRect

    http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGGeometry/Reference ...

  8. IOS开发中的CGFloat、CGPoint、CGSize和CGRect

    IOS开发中的CGFloat.CGPoint.CGSize和CGRect http://developer.apple.com/library/ios/#documentation/GraphicsI ...

  9. The official raywenderlich.com Objective-C style guide.

    The official raywenderlich.com Objective-C style guide. This style guide outlines the coding convent ...

随机推荐

  1. 【推导】【贪心】Codeforces Round #431 (Div. 1) A. From Y to Y

    题意:让你构造一个只包含小写字母的可重集,每次可以取两个元素,将它们合并,合并的代价是这两个元素各自的从‘a’到‘z’出现的次数之积的和. 给你K,你构造的可重集必须满足将所有元素合而为一以后,所消耗 ...

  2. 【构造】AtCoder Regular Contest 079 D - Decrease (Contestant ver.)

    从n个t变化到n个t-1,恰好要n步,并且其中每一步的max值都>=t,所以把50个49当成最终局面,从这里开始,根据输入的K计算初始局面即可. #include<cstdio> # ...

  3. iOS 11 安全区域适配

    //解决iOS11,仅实现heightForHeaderInSection,没有实现viewForHeaderInSection方法时,section间距大的问题 [UITableView appea ...

  4. GCC 内联汇编(GCC内嵌ARM汇编规则)

    转:http://smileleeboo.howbbs.com/posts/list/3127/81062.html 更多文档参见:http://pan.baidu.com/s/1eQ7nd8Q 有时 ...

  5. 学会MySQL索引

    原文:https://mp.weixin.qq.com/s/UzWxJ_pVPjU5ip0Z-Y9TdA 什么是索引? 百度百科是这样描述的: 索引是为来加速对表中数据行中的检索而创建的一种分散的数据 ...

  6. NAT和SNAT

    在Kubernetes负载均衡的方案中遇到了SNAT的问题,查资料把SNAT和NAT的大意了解一下 详细信息可以直接访问 https://support.f5.com/kb/en-us/product ...

  7. void fun() const{}; const void fun(){}; 和void const fun(){}; 的区别?

    void fun() const{}; const void fun(){}; 和void const fun(){}; 的区别? const void fun(){};和void const fun ...

  8. [转]SSIS ProtectionLevel 对包中敏感数据的访问控制

    本文转自:http://technet.microsoft.com/zh-cn/library/ms141747.aspx 为了保护 Integration Services 包中的数据,可以设置保护 ...

  9. linux xfs文件系统无法用readdir获取dirent文件类型d_type则用stat获取暨stat函数讲解

    stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat ...

  10. Qt 5.7 亮瞎眼的更新

    Qt 5.7的beta版已经出来了,这将是一个超级重大的更新,主要有几个商业版的模块在GPLv3 open source 版的用户也可以用了,其中包括了两个很炫酷的模块: Qt Charts Qt D ...