美文网首页
iOS:被忽略的CGGeometry

iOS:被忽略的CGGeometry

作者: AprSnow | 来源:发表于2016-12-19 11:46 被阅读60次

CGGeometry

CGGeometry是2D绘图的一个库,定义了基本的几何元素:CGPoint、CGSize、CGRect。

CGRectOffset

CGRect CGRectOffset(CGRect rect, CGFloat dx, CGFloat dy);

返回一个原点在源矩形基础上进行了偏移的矩形。

例子:

CGRect rect = CGRectMake(45, 40, 
                        CGRectGetWidth(self.view.frame) - 90, 
                        CGRectGetWidth(self.view.frame) - 90);
UIView *rectView = [[UIView alloc] init];
[rectView setFrame:rect];
[rectView setBackgroundColor:[UIColor cyanColor]];
[self.view addSubview:rectView];
    
CGRect offRect = CGRectOffset(rect, 10, 10);
UIView *offRectView = [[UIView alloc] init];
[offRectView setFrame:offRect];
[offRectView setBackgroundColor:[UIColor magentaColor]];
[self.view addSubview:offRectView];
CGRectOffset

CGRectInset

CGRect CGRectInset(CGRect rect, CGFloat dx, CGFloat dy);

返回一个与源矩形共中心点的,或大些或小些的新矩形。

矩形将围绕它的中心点进行缩放,左右分别增减dx(总共2dx),上下分别增减 dy(总共 2dy)。

例子:

CGRect insetRect = CGRectInset(rect, 20, 20);
UIView *insetRectView = [[UIView alloc] init];
[insetRectView setFrame:insetRect];
[insetRectView setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:insetRectView];
CGRectInset

CGRectDivide

void CGRectDivide(CGRect rect, CGRect * slice, CGRect * remainder, CGFloat amount, CGRectEdge edge);

将源矩形分为两个子矩形。

CGRectDivide用以下方式将矩形分割为两部分:

  • 传入一个矩形并选择一条edge(上,下,左,右);
  • 平行那个边在矩形里量出amount的长度;
  • edge到量出的amount区域都保存到slice参数中;
  • 剩余的部分保存到remainder参数中。

其中edge参数是一个CGRectEdge枚举类型:

enum CGRectEdge { 
    CGRectMinXEdge, //左 
    CGRectMinYEdge, //上 
    CGRectMaxXEdge, //右 
    CGRectMaxYEdge  //下
}

例子:

CGRect rect1 = CGRectZero;
CGRect rect2 = CGRectZero;
CGRectDivide(rect, &rect1, &rect2, CGRectGetWidth(rect) / 3, CGRectMinXEdge);
    
UIView *rectView1 = [[UIView alloc] init];
[rectView1 setFrame:CGRectOffset(rect1, 0, CGRectGetHeight(rect) + 20)];
[rectView1 setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:rectView1];
    
UIView *rectView2 = [[UIView alloc] init];
[rectView2 setFrame:CGRectOffset(rect2, 0, CGRectGetHeight(rect) + 20)];
[rectView2 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:rectView2];
CGRectMinXEdge

不同CGRectEdge

  • CGRectMinYEdge
  • CGRectMaxXEdge
  • CGRectMaxYEdge

其他

  • CGRectZero: 一个原点在(0, 0),且长宽均为 0 的常数矩形。这个零矩形与CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) 是等价的。
  • CGRectNull: 空矩形。这个会在,比如说,求两个不相交的矩形的相交部分时返回。注意,空矩形不是零矩形
  • CGRectInfinite: 无穷大矩形。
  • CGRectGetHeight: 返回矩形的高度。
  • CGRectGetWidth: 返回矩形的宽度。
  • CGRectIntegral: 返回包围源矩形的最小整数矩形。
  • CGRectGet[Min|Mid|Max][X|Y]:返回矩形x或y的最小、中间或最大值。

参考

相关文章

  • iOS:被忽略的CGGeometry

    CGGeometry CGGeometry是2D绘图的一个库,定义了基本的几何元素:CGPoint、CGSize、...

  • iOS CGGeometry

    CGGeometry是用C语言实现的一个封装了许多常用几何方法的文件 结构体 全局常量 获取几何参数 判断几何 C...

  • iOS(CGGeometry)几何类方法

    本文转载自这篇博客. 看到一篇文章总结了iOS中CGGeometry的方法, 里面除了我们常用的CGPoint, ...

  • CGGeometry -CGGeometry.h

    CGGeometry CGGeometry.h 变换 CGRectOffset 返回一个原点在原矩形上进行了偏移的...

  • CGGeometry

    参考文章:CGGeometry GRect 用于表示屏幕上绘制的所有视图的 frame,一个程序员操作矩形几何体的...

  • CGGeometry

  • iOS 开发CGRect系列方法

    使用CGGeometry方法获取CGRect数据

  • iOS 被忽略的main函数

    如同任何基于C的应用程序,程序启动的主入口点为iOS应用程序的main函数。在iOS应用程序,main函数的作用是...

  • IOS(CGGeometry)几何类方法总结

    IOS开发几何类方法总结CGGeometry.h文件是用C语言实现的一个封装了许多常用几何方法的文件。一、几个常用...

  • IOS(CGGeometry)几何类方法汇总

    CGGeometry.h文件是用C语言实现的一个封装了许多常用几何方法的文件。

网友评论

      本文标题:iOS:被忽略的CGGeometry

      本文链接:https://www.haomeiwen.com/subject/fyxtvttx.html