CGGeometry Reference
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 结构体转换为一个 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 ...
随机推荐
- 【推导】Codeforces Round #411 (Div. 1) B. Minimum number of steps
最后肯定是bbbb...aaaa...这样. 你每进行一系列替换操作,相当于把一个a移动到右侧. 会增加一些b的数量……然后你统计一下就行.式子很简单. 喵喵喵,我分段统计的,用了等比数列……感觉智障 ...
- redis源码解析之dict数据结构
dict 是redis中最重要的数据结构,存放结构体redisDb中. typedef struct dict { dictType *type; void *privdata; dictht ht[ ...
- 2015 百度之星 1003 棋盘占领 dfs
棋盘占领 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest_show ...
- 【泡咖啡1】linux下caffe编译以及python环境配置手记
caffe是一个深度学习的库,相信搞深度学习的话,不是用这个库就是用theano吧.要想使用caffe首先第一步就是要配置好caffe的环境.在这里,我主要说的是在debian的linux环境下如何配 ...
- Hiho----有向图欧拉回路
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho破解了一道又一道难题,终于来到了最后一关.只要打开眼前的宝箱就可以通关这个游戏了. 宝箱被一种奇怪的机关锁住 ...
- Ubuntu 16.04修改MAC地址以及网络常用设置(IP/DNS/网关)
1.先停止桌面版自带的NetworkManager,这东西很难用,且有些设置需要重启. sudo systemctl stop NetworkManager.service sudo systemct ...
- GNU C内联汇编(AT&T语法)
转:http://www.linuxso.com/linuxbiancheng/40050.html 内联汇编提供了可以在C或C++代码中创建汇编语言代码,不必连接额外的库或程序.这种方法对最终程序在 ...
- Android 花钱 划动手指每天一元钱
花钱(英文ColorMoney)是由上海花动传媒开发的一款免费的应用程序,现支持Android操作系统.安装花钱app后,用户将获得全新的手机锁屏背景图,其中包含各种有趣.唯美的壁纸类图片,亦包含应用 ...
- Spring @Autowired 注解不生效
@Autowired默认不生效.为了生效,需要在xml配置:<context:annotation-config> 注解一<context:component-scan base-p ...
- 剖析ASP.NET Core(Part 2)- AddMvc(译)
原文:https://www.stevejgordon.co.uk/asp-net-core-mvc-anatomy-addmvccore发布于:2017年3月环境:ASP.NET Core 1.1 ...