CGGeometry Reference
CGRectUnionCGRectUnion接受两个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来计算红色和橙色矩形的话,你可能要多谢几十行代码。不信你就试试。
比较和包含
用下面六个方法来比较几何结构和检查包含关系非常简单。
CGPointEqualToPointCGSizeEqualToSizeCGRectEqualToRectCGRectIntersectsRectCGRectContainsPointCGRectContainsRect
CGGeometry Reference 还有一些其他宝贝,比如CGPointCreateDictionaryRepresentation可以用来将一个 CGPoint 结构体转换为一个 CGDictionaryRef,CGRectIsEmpty可以用来检查一个矩形的宽高是否都为零。更多详情请看[《CGGeometry Reference 文档》]()
来源:https://segmentfault.com/a/1190000004695617?hmsr=toutiao.io
CGGeometry Reference的更多相关文章
- CGGeometry Reference (一)
知识点 frame 与bounds 的区别 1.frame 是这个视图的大小在父视图的位置 . 如x 20 y 20 width 200 height 300 2.bounds 是这个视图的大小在自 ...
- iOS 基础:Frames、Bounds 和 CGGeometry
https://segmentfault.com/a/1190000004695617 原文:<iOS Fundamentals: Frames, Bounds, and CGGeometry& ...
- iOS编码规范
The official raywenderlich.com Objective-C style guide. This style guide outlines the coding con ...
- IOS开发-代码规范
代码风格的重要性对于一个团队和项目来说不言而喻.网上有许多 Objective-C 的代码风格,但这份简洁而又最符合苹果的规范,同时有助于养成良好的代码习惯,也是我们团队一直遵循的代码风格. 写法没有 ...
- 官方的objective - c风格指南。
The official raywenderlich.com Objective-C style guide. This style guide outlines the coding convent ...
- Objective-C 编码风格指南
本文转自:[Objective-C 编码风格指南 | www.samirchen.com][2] ## 背景 保证自己的代码遵循团队统一的编码规范是一个码农的基本节操,能够进入一个有统一编码规范的团队 ...
- [转]IOS开发中的CGFloat、CGPoint、CGSize和CGRect
http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGGeometry/Reference ...
- IOS开发中的CGFloat、CGPoint、CGSize和CGRect
IOS开发中的CGFloat.CGPoint.CGSize和CGRect http://developer.apple.com/library/ios/#documentation/GraphicsI ...
- The official raywenderlich.com Objective-C style guide.
The official raywenderlich.com Objective-C style guide. This style guide outlines the coding convent ...
随机推荐
- 【枚举】【尺取法】hdu6103 Kirinriki
两个等长字符串A,B的距离被定义为 给你一个字符串,问你对于所有长度相等的不相交子串对,其距离不超过m的前提下,最长的长度是多少. 枚举对称轴,两侧先贪心地扩展到最长,超过m之后,再缩短靠近对称轴的端 ...
- 【优先队列】POJ1442-Black Box
[思路] 建立一个小堆和一个大堆.大堆用来存放第1..index-1大的数,其余数存放在大堆,小堆的堆顶元素便是我们要求出的第index大的数.每次插入一个A(n),必须保证大堆中数字数目不变,故先插 ...
- k-近邻算法 简单例子
from numpy import * import operator def create_data_set(): # 训练集与标签 group = array([[1.0, 1.1], [1.0, ...
- Problem A: 自定义函数strcomp(),实现两个字符串的比较
#include<stdio.h> int strcmp(char *str1,char *str2) { if(str1!=NULL&&str2!=NULL) { whi ...
- Java高级架构师(一)第36节:Nginx的反向代理模块
理解Http正向代理和Http反向代理的区别 Proxy模块,最常用的proxy_pass, Proxy_pass 可以转发请求到其他的浏览器. # Nginx强项在于负载.反向.动静分离 #
- iOS 在系统设置中展示Version, Build, Git等信息
在设置中,展示自定义内容,如下图INFO区域内容: 步骤: 1.在项目中添加Settings.bundle文件 Root.plist和Root.plist的Source code如下 ...
- mysql锁机制整理
Auth: jinDate: 20140506 主要参考整理资料MYSQL性能调优与架构设计-第七章 MYSQL锁定机制http://www.cnblogs.com/ggjucheng/archive ...
- C#反射读取和设置类的属性
C#反射技术的简单操作(读取和设置类的属性) http://www.cnblogs.com/william-lin/archive/2013/06/05/3118233.html 泛型方法通过反射创建 ...
- Vue2.X的路由管理记录之 钩子函数(切割流水线)2
$route可以在子组件任何地方调用,代表当前路由对象,这个属性是只读的,里面的属性是 immutable(不可变) 的,不过你可以 watch(监测变化) 它. 导航和钩子函数: 导航:路由正在发生 ...
- SQL 的四种分类 DDL,DML,DCL,TCL
DDL (数据定义问题) 数据定义语言 - Data Definition Language 用来定义数据库的对象,如数据表.视图.索引等DDL不需要commit.CREATEALTERDROPTRU ...