UIBezierPath

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

笔者在学习时,首先看了两遍UIBezierPath类头文件定义,熟悉了一下相关的属性和方法。

基础知识


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

UIBezierPathCGPathRef数据类型的封装。如果是基于矢量形状的路径,都用直线和曲线去创建。我们使用直线段去创建矩形和多边形,使用曲线去创建圆弧(arc)、圆或者其他复杂的曲线形状。

使用UIBezierPath画图步骤:

  1. 创建一个UIBezierPath对象
  2. 调用-moveToPoint:设置初始线段的起点
  3. 添加线或者曲线去定义一个或者多个子路径
  4. 改变UIBezierPath对象跟绘图相关的属性。如,我们可以设置画笔的属性、填充样式等

UIBezierPath创建方法介绍


我们先看看UIBezierPath类提供了哪些创建方式,这些都是工厂方法,直接使用即可。

  1. + (instancetype)bezierPath;
  2. + (instancetype)bezierPathWithRect:(CGRect)rect;
  3. + (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
  4. + (instancetype)bezierPathWithRoundedRect:(CGRect)rect
  5. cornerRadius:(CGFloat)cornerRadius;
  6. + (instancetype)bezierPathWithRoundedRect:(CGRect)rect
  7. byRoundingCorners:(UIRectCorner)corners
  8. cornerRadii:(CGSize)cornerRadii;
  9. + (instancetype)bezierPathWithArcCenter:(CGPoint)center
  10. radius:(CGFloat)radius
  11. startAngle:(CGFloat)startAngle
  12. endAngle:(CGFloat)endAngle
  13. clockwise:(BOOL)clockwise;
  14. + (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

下面我们一个一个地介绍其用途。

  1. + (instancetype)bezierPath;

这个使用比较多,因为这个工厂方法创建的对象,我们可以根据我们的需要任意定制样式,可以画任何我们想画的图形。

  1. + (instancetype)bezierPathWithRect:(CGRect)rect;

这个工厂方法根据一个矩形画贝塞尔曲线。

  1. + (instancetype)bezierPathWithOvalInRect:(CGRect)rect;

这个工厂方法根据一个矩形画内切曲线。通常用它来画圆或者椭圆。

  1. + (instancetype)bezierPathWithRoundedRect:(CGRect)rect
  2. cornerRadius:(CGFloat)cornerRadius;
  3. + (instancetype)bezierPathWithRoundedRect:(CGRect)rect
  4. byRoundingCorners:(UIRectCorner)corners
  5. cornerRadii:(CGSize)cornerRadii;

第一个工厂方法是画矩形,但是这个矩形是可以画圆角的。第一个参数是矩形,第二个参数是圆角大小。
第二个工厂方法功能是一样的,但是可以指定某一个角画成圆角。像这种我们就可以很容易地给UIView扩展添加圆角的方法了。

  1. + (instancetype)bezierPathWithArcCenter:(CGPoint)center
  2. radius:(CGFloat)radius
  3. startAngle:(CGFloat)startAngle
  4. endAngle:(CGFloat)endAngle
  5. clockwise:(BOOL)clockwise;

这个工厂方法用于画弧,参数说明如下:
center: 弧线中心点的坐标
radius: 弧线所在圆的半径
startAngle: 弧线开始的角度值
endAngle: 弧线结束的角度值
clockwise: 是否顺时针画弧线

温馨提示:我们下面的代码都是在自定义的BezierPathView类中的- (void)drawRect:(CGRect)rect方法中调用

画三角形


先看效果图:

image
  1. // 画三角形
  2. - (void)drawTrianglePath {
  3. UIBezierPath *path = [UIBezierPath bezierPath];
  4. [path moveToPoint:CGPointMake(20, 20)];
  5. [path addLineToPoint:CGPointMake(self.frame.size.width - 40, 20)];
  6. [path addLineToPoint:CGPointMake(self.frame.size.width / 2, self.frame.size.height - 20)];
  7. // 最后的闭合线是可以通过调用closePath方法来自动生成的,也可以调用-addLineToPoint:方法来添加
  8. // [path addLineToPoint:CGPointMake(20, 20)];
  9. [path closePath];
  10. // 设置线宽
  11. path.lineWidth = 1.5;
  12. // 设置填充颜色
  13. UIColor *fillColor = [UIColor greenColor];
  14. [fillColor set];
  15. [path fill];
  16. // 设置画笔颜色
  17. UIColor *strokeColor = [UIColor blueColor];
  18. [strokeColor set];
  19. // 根据我们设置的各个点连线
  20. [path stroke];
  21. }

我们设置画笔颜色通过set方法:

  1. UIColor *strokeColor = [UIColor blueColor];
  2. [strokeColor set];

如果我们需要设置填充颜色,比如这里设置为绿色,那么我们需要在设置画笔颜色之前先设置填充颜色,否则画笔颜色就被填充颜色替代了。也就是说,如果要让填充颜色与画笔颜色不一样,那么我们的顺序必须是先设置填充颜色再设置画笔颜色。如下,这两者顺序不能改变。因为我们设置填充颜色也是跟设置画笔颜色一样调用UIColor-set方法。

  1. // 设置填充颜色
  2. UIColor *fillColor = [UIColor greenColor];
  3. [fillColor set];
  4. [path fill];
  5. // 设置画笔颜色
  6. UIColor *strokeColor = [UIColor blueColor];
  7. [strokeColor set];

画矩形


先看效果图:

image
  1. // 画矩形
  2. - (void)drawRectPath {
  3. UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.height - 40)];
  4. path.lineWidth = 1.5;
  5. path.lineCapStyle = kCGLineCapRound;
  6. path.lineJoinStyle = kCGLineJoinBevel;
  7. // 设置填充颜色
  8. UIColor *fillColor = [UIColor greenColor];
  9. [fillColor set];
  10. [path fill];
  11. // 设置画笔颜色
  12. UIColor *strokeColor = [UIColor blueColor];
  13. [strokeColor set];
  14. // 根据我们设置的各个点连线
  15. [path stroke];
  16. }

lineCapStyle属性是用来设置线条拐角帽的样式的,其中有三个选择:

  1. /* Line cap styles. */
  2. typedef CF_ENUM(int32_t, CGLineCap) {
  3. kCGLineCapButt,
  4. kCGLineCapRound,
  5. kCGLineCapSquare
  6. };

其中,第一个是默认的,第二个是轻微圆角,第三个正方形。

lineJoinStyle属性是用来设置两条线连结点的样式,其中也有三个选择:

  1. /* Line join styles. */
  2. typedef CF_ENUM(int32_t, CGLineJoin) {
  3. kCGLineJoinMiter,
  4. kCGLineJoinRound,
  5. kCGLineJoinBevel
  6. };

其中,第一个是默认的表示斜接,第二个是圆滑衔接,第三个是斜角连接。

画圆


我们可以使用+ bezierPathWithOvalInRect:方法来画圆,当我们传的rect参数是一下正方形时,画出来的就是圆。

先看效果图:

image
  1. - (void)drawCiclePath {
  2. // 传的是正方形,因此就可以绘制出圆了
  3. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.width - 40)];
  4. // 设置填充颜色
  5. UIColor *fillColor = [UIColor greenColor];
  6. [fillColor set];
  7. [path fill];
  8. // 设置画笔颜色
  9. UIColor *strokeColor = [UIColor blueColor];
  10. [strokeColor set];
  11. // 根据我们设置的各个点连线
  12. [path stroke];
  13. }

注意:要画圆,我们需要传的rect参数必须是正方形哦!

画椭圆


先看效果图:

image

前面我们已经画圆了,我们可以使用+ bezierPathWithOvalInRect:方法来画圆,当我们传的rect参数是一下正方形时,画出来的就是圆。那么我们要是不传正方形,那么绘制出来的就是椭圆了。

  1. // 画椭圆
  2. - (void)drawOvalPath {
  3. // 传的是不是正方形,因此就可以绘制出椭圆圆了
  4. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, self.frame.size.width - 80, self.frame.size.height - 40)];
  5. // 设置填充颜色
  6. UIColor *fillColor = [UIColor greenColor];
  7. [fillColor set];
  8. [path fill];
  9. // 设置画笔颜色
  10. UIColor *strokeColor = [UIColor blueColor];
  11. [strokeColor set];
  12. // 根据我们设置的各个点连线
  13. [path stroke];
  14. }

画带圆角的矩形


  1. + (instancetype)bezierPathWithRoundedRect:(CGRect)rect
  2. cornerRadius:(CGFloat)cornerRadius;
  3. + (instancetype)bezierPathWithRoundedRect:(CGRect)rect
  4. byRoundingCorners:(UIRectCorner)corners
  5. cornerRadii:(CGSize)cornerRadii;

第一个工厂方法是画矩形,但是这个矩形是可以画圆角的。第一个参数是矩形,第二个参数是圆角大小。
第二个工厂方法功能是一样的,但是可以指定某一个角画成圆角。像这种我们就可以很容易地给UIView扩展添加圆角的方法了。

四个都是圆角10:

image
  1. - (void)drawRoundedRectPath {
  2. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.height - 40) cornerRadius:10];
  3. // 设置填充颜色
  4. UIColor *fillColor = [UIColor greenColor];
  5. [fillColor set];
  6. [path fill];
  7. // 设置画笔颜色
  8. UIColor *strokeColor = [UIColor blueColor];
  9. [strokeColor set];
  10. // 根据我们设置的各个点连线
  11. [path stroke];
  12. }

如果要画只有一个角是圆角,那么我们就修改创建方法:

  1. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.height - 40) byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];

其中第一个参数一样是传了个矩形,第二个参数是指定在哪个方向画圆角,第三个参数是一个CGSize类型,用来指定水平和垂直方向的半径的大小。看下效果图:

image

画弧


画弧前,我们需要了解其参考系,如下图(图片来自网络):

image
  1. #define kDegreesToRadians(degrees) ((pi * degrees)/ 180)
  2. - (void)drawARCPath {
  3. const CGFloat pi = 3.14159265359;
  4. CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
  5. UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center
  6. radius:100
  7. startAngle:0
  8. endAngle:kDegreesToRadians(135)
  9. clockwise:YES];
  10. path.lineCapStyle = kCGLineCapRound;
  11. path.lineJoinStyle = kCGLineJoinRound;
  12. path.lineWidth = 5.0;
  13. UIColor *strokeColor = [UIColor redColor];
  14. [strokeColor set];
  15. [path stroke];
  16. }

效果图如下:

image

我们要明确一点,画弧参数startAngleendAngle使用的是弧度,而不是角度,因此我们需要将常用的角度转换成弧度。对于效果图中,我们设置弧的中心为控件的中心,起点弧度为0,也就是正东方向,而终点是135度角的位置。如果设置的clockwise:YES是逆时针方向绘制,如果设置为NO,效果如下:

image

这两者正好是相反的。

画二次贝塞尔曲线


先来学习一下关于控制点,如下图(图片来自网络):

image

画二次贝塞尔曲线,是通过调用此方法来实现的:

  1. - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint

参数说明:

endPoint:终端点<br/>
controlPoint:控制点,对于二次贝塞尔曲线,只有一个控制点

看效果图:

image
  1. - (void)drawSecondBezierPath {
  2. UIBezierPath *path = [UIBezierPath bezierPath];
  3. // 首先设置一个起始点
  4. [path moveToPoint:CGPointMake(20, self.frame.size.height - 100)];
  5. // 添加二次曲线
  6. [path addQuadCurveToPoint:CGPointMake(self.frame.size.width - 20, self.frame.size.height - 100)
  7. controlPoint:CGPointMake(self.frame.size.width / 2, 0)];
  8. path.lineCapStyle = kCGLineCapRound;
  9. path.lineJoinStyle = kCGLineJoinRound;
  10. path.lineWidth = 5.0;
  11. UIColor *strokeColor = [UIColor redColor];
  12. [strokeColor set];
  13. [path stroke];
  14. }

画二次贝塞尔曲线的步骤:

  1. 先设置一个起始点,也就是通过-moveToPoint:设置
  2. 调用-addQuadCurveToPoint:controlPoint:方法设置终端点和控制点,以画二次曲线

在效果图中,拱桥左边的起始点就是我们设置的起始点,最右边的终点,就是我们设置的终端点,而我们设置的控制点为(width / 2, 0)对应于红色矩形中水平方向在正中央,而垂直方向在最顶部。

这个样式看起来很像sin或者cos函数吧?这两个只是特例而已,其实可以画任意图形,只是想不到,没有做不到的。

画三次贝塞尔曲线


贝塞尔曲线必定通过首尾两个点,称为端点;中间两个点虽然未必要通过,但却起到牵制曲线形状路径的作用,称作控制点。关于三次贝塞尔曲线的控制器,看下图:

image

提示:其组成是起始端点+控制点1+控制点2+终止端点

如下方法就是画三次贝塞尔曲线的关键方法,以三个点画一段曲线,一般和-moveToPoint:配合使用。

  1. - (void)addCurveToPoint:(CGPoint)endPoint
  2. controlPoint1:(CGPoint)controlPoint1
  3. controlPoint2:(CGPoint)controlPoint2

看下效果图:

image

实现代码是这样的:

  1. - (void)drawThirdBezierPath {
  2. UIBezierPath *path = [UIBezierPath bezierPath];
  3. // 设置起始端点
  4. [path moveToPoint:CGPointMake(20, 150)];
  5. [path addCurveToPoint:CGPointMake(300, 150)
  6. controlPoint1:CGPointMake(160, 0)
  7. controlPoint2:CGPointMake(160, 250)];
  8. path.lineCapStyle = kCGLineCapRound;
  9. path.lineJoinStyle = kCGLineJoinRound;
  10. path.lineWidth = 5.0;
  11. UIColor *strokeColor = [UIColor redColor];
  12. [strokeColor set];
  13. [path stroke];
  14. }

我们需要注意,这里确定的起始端点为(20,150),终止端点为(300, 150),基水平方向是一致的。控制点1的坐标是(160,0),水平方向相当于在中间附近,这个参数可以调整。控制点2的坐标是(160,250),如果以两个端点的连线为水平线,那么就是250-150=100,也就是在水平线下100。这样看起来就像一个sin函数了。

UIBezierPath的更多相关文章

  1. 使用UIBezierPath绘制图形

    当需要画图时我们一般创建一个UIView子类, 重写其中的drawRect方法 再drawRect方法中利用UIBezierPath添加画图 UIBezierPath的使用方法: (1)创建一个Bez ...

  2. 贝塞尔曲线(UIBezierPath)属性、方法汇总

    UIBezierPath主要用来绘制矢量图形,它是基于Core Graphics对CGPathRef数据类型和path绘图属性的一个封装,所以是需要图形上下文的(CGContextRef),所以一般U ...

  3. 贝赛尔曲线UIBezierPath(后续)

    使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...

  4. 贝赛尔曲线UIBezierPath

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

  5. Swift - UIBezierPath

    使用UIBezierPath可以创建基于矢量的路径.使用此类可以定义简单的形状,如椭圆.矩形或者有多个直线和曲线段组成的形状等.主要用到的该类的属性包括 moveToPoint: //设置起始点 ad ...

  6. IOS Animation-CAShapeLayer、UIBezierPath与Animation的结合

    在阅读本文之前,对CAShapeLayer.UIBezierPath不熟悉的话,可以先阅读文章 贝塞尔曲线与Layer 如果对动画不熟悉的话,先阅读文章 动画基础.深入 Layer是绘图的画板,Bez ...

  7. iOS关于CAShapeLayer与UIBezierPath的知识内容

    使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...

  8. iOS - 用 UIBezierPath 实现果冻效果

    最近在网上看到一个很酷的下拉刷新效果(http://iostuts.io/2015/10/17/elastic-bounce-using-uibezierpath-and-pan-gesture/). ...

  9. UIBezierPath 的使用

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

  10. UIBezierPath类 笔记

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

随机推荐

  1. 解决WIN7上 SQL2008r2 由于防火墙问题 客户端无法远程连接的问题

    打开防火墙->入站规则->新建规则->选择端口  TCP 1433 允许->... OVER

  2. H - Antenna Placement- hdu 3020(二分图匹配)

    题意:每个 ‘*’都需要一个1*2的东西覆盖,问最少需要多少个1*2的东西来覆盖这些‘*’ 分析:只需要求出来最多有多少个完全覆盖的,然后加上那些不能被完全覆盖的点即可..把G题的代码随便修改了一下就 ...

  3. L - Vases and Flowers - hdu 4614(区间操作)

    题意:有两种操作,第一种从A开始插花,如果有花就跳到下一个,然后输出最后一个花瓶的编号,如果花瓶不够把多余的花丢掉.操作2把区间清空 分析:很明显的线段树操作,就是插花的时候麻烦一下,需要先找出来他剩 ...

  4. java中字符串的比较

    compareTo方法是比较两个字符串的词典顺序  也就是在字典中的顺序,比如“abcd”在“acdb”前面 大于返回1,小于返回-1 equals:比较两字符串的内容是否相同. 相同返回1,不同返回 ...

  5. java异常面试常见题目

    在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题.Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它 ...

  6. 【转】使用 NuGet 管理项目库-Phil Haack

    原文地址:https://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 ...

  7. sql server 扩展存储过程

    C# 代码 using Microsoft.SqlServer.Server; using System; using System.Collections.Generic; using System ...

  8. JS~字符串长度判断,超出进行自动截取(支持中文)

    今天一个小弟问我的问题,在文本框中输入字符,如果超出指定长度,就把它截取,要求中文等于两个字符的长度,我找一下资料,把这个功能实现了,下面是JS代码: <html> <script ...

  9. linux怎么给一个普通用户reboot权限?

    分四种情况讨论:1.让任何人(包括根本不拥有系统帐号的人)都可以通过控制台reboot在/etc/inittab文件中保留ca::ctrlaltdel:/sbin/shutdown -t3 -r no ...

  10. Java基础知识强化之集合框架笔记02:集合的继承体系图解

    1. 集合的继承体系图解: