前言

UIBezierPathUIKit中的一个关于图形绘制的类,是通过Quartz 2D也就是CG(Core Graphics)CGPathRef的封装得到的,从高级特性支持来看不及CG

UIBezierPath类可以绘制矩形、圆形、直线、曲线以及它们的组合图形。

UIBezierPath对象

对象创建方法

// 创建基本路径
+ (instancetype)bezierPath;
// 创建矩形路径
+ (instancetype)bezierPathWithRect:(CGRect)rect;
// 创建椭圆路径
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
// 创建圆角矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
// 创建指定位置圆角的矩形路径
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
// 创建弧线路径
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
// 通过CGPath创建
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

相关属性和方法

  • 属性
// 与之对应的CGPath
@property(nonatomic) CGPathRef CGPath;
- (CGPathRef)CGPath NS_RETURNS_INNER_POINTER CF_RETURNS_NOT_RETAINED;
// 是否为空
@property(readonly,getter=isEmpty) BOOL empty;
// 整个路径相对于原点的位置及宽高
@property(nonatomic,readonly) CGRect bounds;
// 当前画笔位置
@property(nonatomic,readonly) CGPoint currentPoint;
// 线宽
@property(nonatomic) CGFloat lineWidth;// 终点类型
@property(nonatomic) CGLineCap lineCapStyle;
typedef CF_ENUM(int32_t, CGLineCap) {kCGLineCapButt,kCGLineCapRound,kCGLineCapSquare
};// 交叉点的类型
@property(nonatomic) CGLineJoin lineJoinStyle;
typedef CF_ENUM(int32_t, CGLineJoin) {kCGLineJoinMiter,kCGLineJoinRound,kCGLineJoinBevel
};// 两条线交汇处内角和外角之间的最大距离,需要交叉点类型为kCGLineJoinMiter是生效,最大限制为10
@property(nonatomic) CGFloat miterLimit;
// 个人理解为绘线的精细程度,默认为0.6,数值越大,需要处理的时间越长
@property(nonatomic) CGFloat flatness;
// 决定使用even-odd或者non-zero规则
@property(nonatomic) BOOL usesEvenOddFillRule;
  • 方法

// 反方向绘制path

- (UIBezierPath *)bezierPathByReversingPath;

// 设置画笔起始点

- (void)moveToPoint:(CGPoint)point;

// 从当前点到指定点绘制直线

- (void)addLineToPoint:(CGPoint)point;

// 添加弧线

- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);
// center弧线圆心坐标 radius弧线半径 startAngle弧线起始角度 endAngle弧线结束角度 clockwise是否顺时针绘制

// 添加贝塞尔曲线

- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
// endPoint终点 controlPoint控制点
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
// endPoint终点 controlPoint1、controlPoint2控制点

// 移除所有的点,删除所有的subPath

- (void)removeAllPoints;

// 将bezierPath添加到当前path

- (void)appendPath:(UIBezierPath *)bezierPath;

// 填充

- (void)fill;

// 路径绘制

- (void)stroke;

// 在这以后的图形绘制超出当前路径范围则不可见

- (void)addClip;

直线

- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];UIBezierPath* path = [UIBezierPath bezierPath];path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;// 起点[path moveToPoint:CGPointMake(20, 100)];// 绘制线条[path addLineToPoint:CGPointMake(200, 20)];[path stroke];
}

矩形

  • 直角矩形
- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];// 创建矩形路径对象UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 150, 100)];path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;[path stroke];
}
  • 圆角矩形
- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];// 创建圆角矩形路径对象UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 150, 100) cornerRadius:30]; // 圆角半径为30path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;[path stroke];
}
  • 指定位置圆角矩形
- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 150, 100) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(30, 30)];path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;[path stroke];
}

corners:圆角位置 cornerRadii:圆角大小

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {UIRectCornerTopLeft     = 1 << 0,UIRectCornerTopRight    = 1 << 1,UIRectCornerBottomLeft  = 1 << 2,UIRectCornerBottomRight = 1 << 3,UIRectCornerAllCorners  = ~0UL
};

圆形和椭圆形

  • 圆形
- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];// 创建圆形路径对象UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;[path stroke];
}
  • 椭圆形
- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];// 创建椭圆形路径对象UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;[path stroke];
}

曲线

  • 弧线
- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];// 创建弧线路径对象UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100)radius:70startAngle:3.1415926endAngle:3.1415926 *3/2clockwise:YES];path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;[path stroke];
}

center:弧线圆心坐标
radius:弧线半径
startAngle:弧线起始角度
endAngle:弧线结束角度
clockwise:是否顺时针绘制

默认坐标系统中的角度值
  • 贝塞尔曲线1
- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];UIBezierPath* path = [UIBezierPath bezierPath];path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;[path moveToPoint:CGPointMake(20, 100)];// 给定终点和控制点绘制贝塞尔曲线[path addQuadCurveToPoint:CGPointMake(150, 100) controlPoint:CGPointMake(20, 0)];[path stroke];
}
  • 贝塞尔曲线2
- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];UIBezierPath* path = [UIBezierPath bezierPath];path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;[path moveToPoint:CGPointMake(20, 100)];// 给定终点和两个控制点绘制贝塞尔曲线[path addCurveToPoint:CGPointMake(220, 100) controlPoint1:CGPointMake(120, 20) controlPoint2:CGPointMake(120, 180)];[path stroke];
}

扇形

- (void)drawRect:(CGRect)rect{[[UIColor redColor] set]; // 画笔颜色设置UIBezierPath * path = [UIBezierPath bezierPath]; // 创建路径[path moveToPoint:CGPointMake(100, 100)]; // 设置起始点[path addArcWithCenter:CGPointMake(100, 100) radius:75 startAngle:0 endAngle:3.14159/2 clockwise:NO]; // 绘制一个圆弧path.lineWidth     = 5.0;path.lineCapStyle  = kCGLineCapRound; //线条拐角path.lineJoinStyle = kCGLineCapRound; //终点处理[path closePath]; // 封闭未形成闭环的路径[path stroke]; // 绘制
}

多边形

- (void)drawRect:(CGRect)rect{[[UIColor redColor] set];UIBezierPath* path = [UIBezierPath bezierPath];path.lineWidth     = 5.f;path.lineCapStyle  = kCGLineCapRound;path.lineJoinStyle = kCGLineCapRound;// 起点[path moveToPoint:CGPointMake(100, 50)];// 添加直线[path addLineToPoint:CGPointMake(150, 50)];[path addLineToPoint:CGPointMake(200, 100)];[path addLineToPoint:CGPointMake(200, 150)];[path addLineToPoint:CGPointMake(150, 200)];[path addLineToPoint:CGPointMake(100, 200)];[path addLineToPoint:CGPointMake(50, 150)];[path addLineToPoint:CGPointMake(50, 100)];[path closePath];//根据坐标点连线[path stroke];[path fill];
}

参考:
http://www.jianshu.com/p/60aad4957923
http://www.jianshu.com/p/bbb2cc485a45

UIBezierPath介绍相关推荐

  1. iOS: UIBezierPath简介及静态图形实现

    UIBezierPath介绍 基本介绍: UIBezierPath可以用来创建矢量的路径和图形,使用此类可以定义各种图形.我们用直线和弧形的组合来创建复杂的图形.每一个直线段或者曲线段的结束的地方是下 ...

  2. UIBezierPath 的使用介绍

      使用UIBezierPath类可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状. 1 ...

  3. 使用 UIBezierPath 进行简单的图形绘制

    这篇文章介绍UIBezierPath的详细的使用, 以及一些细节! 创建一个XTBezierPath继承于UIView的类 使用drawRect 完成图形的绘制 在drawRect方法完成绘制 使用  ...

  4. UIBezierPath使用

    UIBezierPath使用 贝塞尔曲线作用 贝塞尔曲线路径可用来绘制自定义路径,圆,弧度,矩形,单独圆角矩形等 UIBerzierPath类介绍 初始化方法 /// MARK: - 初始化方法[常规 ...

  5. ios 贝塞尔曲线 颜色填充_IOS 贝塞尔曲线(UIBezierPath)属性、方法整理

    IOS 贝塞尔曲线详解 开发IOS的朋友都知道IOS 贝塞尔曲线的重要性,由于经常会用到这样的东西,索性抽时间就把相应所有的属性,方法做一个总结. UIBezierPath主要用来绘制矢量图形,它是基 ...

  6. iOS quartzCore学习之UIBezierPath 详解

    转自: http://blog.csdn.net/hdfqq188816190/article/details/51435219 UIBezierPath主要用来绘制矢量图形,它是基于Core Gra ...

  7. UIBezierPath

    学习UIBezierPath画图 笔者在写本篇文章之前,也没有系统学习过贝塞尔曲线,只是曾经某一次的需求需要使用到,才临时百度看了一看而且使用最基本的功能.现在总算有时间停下来好好研究研究这个神奇而伟 ...

  8. ios 贝塞尔曲线 颜色填充_iOS贝塞尔曲线(UIBezierPath)的基本使用方法

    简介 UIBezierPath是对Core Graphics框架的一个封装,使用UIBezierPath类我们可以画出圆形(弧线)或者多边形(比如:矩形)等形状,所以在画复杂图形的时候会经常用到. 分 ...

  9. ios 裁剪圆形头像_IOS_iOS如何裁剪圆形头像,本文实例为大家介绍了iOS裁剪 - phpStudy...

    iOS如何裁剪圆形头像 本文实例为大家介绍了iOS裁剪圆形头像的详细代码,供大家参考,具体内容如下 - (void)viewDidLoad { [super viewDidLoad]; //加载图片 ...

最新文章

  1. 在asp.net中调用Office来制作各种(3D)统计图
  2. java多线程发牌 一个发牌 三个玩家_JAVA代码之斗地主发牌
  3. UICollectionView(一)基本概念
  4. 今天我解决的sql中文乱码问题
  5. 如果计算机用户有密码 待机,电脑待机密码怎么设置
  6. 微软自带iscsi客户端对iqn的要求
  7. python 操作word文字加粗、变红_使用python-docx在表格中使单元格加粗
  8. Spring Boot 2.0.3 使用外置 Tomcat 服务器
  9. 【分享】VISIO 2003下载
  10. 关于logo1_.exe(威金病毒)蠕虫病毒的清楚,
  11. hdu 5208 Where is Bob
  12. Andriod1.0无法被识别,更新为安卓 ADB 驱动
  13. 嗨格式Heic图片转换器v1.0.13.1436官方版
  14. 【07】概率图推断之信念传播
  15. XTUOJ-1299-String
  16. dateframe取某列数据_DataFrame数据选取全攻略
  17. Docker版本变化说明
  18. LeetCode 剪绳子
  19. 模电学习6. 常用的三极管放大电路
  20. 0.54与0.55版本react-native的TextInput在iOS上无法输入中文日文韩文的bug

热门文章

  1. 一作解读:EID-2017-宏基因组测序在新发腹泻病毒鉴定中的应用
  2. Ecology Letters:重金属的污染可导致铁载体生产菌的增加
  3. 如何入门生信Linux
  4. 人体肠道细菌与自身细胞的比例究竟是多少?
  5. php修改数据库字段内容,php对数据库的增删改查操作
  6. R语言ggplot2可视化:ggplot2可视化使用guide_axis(check.overlap=TRUE)选项删除重叠的轴文本、跳过部分中间轴标签
  7. R语言广义加性模型GAMs:可视化每个变量的样条函数、样条函数与变量与目标变量之间的平滑曲线比较、并进行多变量的归一化比较、测试广义线性加性模型GAMs在测试集上的表现(防止过拟合)
  8. R语言explore包进行探索性数据分析实战(EDA、exploratory data analysis):基于iris数据集
  9. Windows下查看已经安装的GPU的情况
  10. pyradiomics的yaml文件参数设置解读、Li‘s have a solution and plan.