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

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

1.使用UIBezierPath画图步骤:

2.创建一个UIBezierPath对象

3.调用-moveToPoint:设置初始线段的起点

4.添加线或者曲线去定义一个或者多个子路径

5.改变UIBezierPath对象跟绘图相关的属性。如,我们可以设置画笔的属性、填充样式等

UIBezierPath创建方法介绍

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

+ (instancetype)bezierPath;

+ (instancetype)bezierPathWithRect:(CGRect)rect;

+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect

cornerRadius:(CGFloat)cornerRadius;

+ (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;

+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

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

+ (instancetype)bezierPath;

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

+ (instancetype)bezierPathWithRect:(CGRect)rect;

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

+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;这个工厂方法根据一个矩形画内切曲线。通常用它来画圆或者椭圆。

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect

cornerRadius:(CGFloat)cornerRadius;

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect

byRoundingCorners:(UIRectCorner)corners

cornerRadii:(CGSize)cornerRadii;

第一个工厂方法是画矩形,但是这个矩形是可以画圆角的。第一个参数是矩形,第二个参数是圆角大小。

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

+ (instancetype)bezierPathWithArcCenter:(CGPoint)center

radius:(CGFloat)radius

startAngle:(CGFloat)startAngle

endAngle:(CGFloat)endAngle

clockwise:(BOOL)clockwise;

这个工厂方法用于画弧,参数说明如下:

center: 弧线中心点的坐标

radius: 弧线所在圆的半径

startAngle: 弧线开始的角度值

endAngle: 弧线结束的角度值

clockwise: 是否顺时针画弧线

以下显示代码都在 -(void)drawRect:(CGRect)rect方法中调用

画三角形

#pragma mark-------- 画三角形

- (void)drawTrianglePath {

UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(20, 20)];

[path addLineToPoint:CGPointMake(self.frame.size.width - 40, 20)];

[path addLineToPoint:CGPointMake(self.frame.size.width / 2, self.frame.size.height - 20)];

// 最后的闭合线是可以通过调用closePath方法来自动生成的,也可以调用-addLineToPoint:方法来添加

//  [path addLineToPoint:CGPointMake(20, 20)];

[path closePath];

// 设置线宽

path.lineWidth = 1.5;

// 设置填充颜色,三角形的内部填充色

UIColor *fillColor = [UIColor greenColor];

[fillColor set];

[path fill];

// 设置画笔颜色

UIColor *strokeColor = [UIColor blueColor];

[strokeColor set];

// 根据我们设置的各个点连线

[path stroke];

}

画矩形

#pragma mark-------- 画矩形

- (void)drawRectPath {

UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, self.frame.size.width - 100, self.frame.size.height - 300)];

path.lineWidth = 1.5;

path.lineCapStyle = kCGLineCapRound;

path.lineJoinStyle = kCGLineJoinBevel;

// 设置填充颜色

UIColor *fillColor = [UIColor greenColor];

[fillColor set];

[path fill];

// 设置画笔颜色

UIColor *strokeColor = [UIColor blueColor];

[strokeColor set];

// 根据我们设置的各个点连线

[path stroke];

}

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

typedef CF_ENUM(int32_t, CGLineCap) {

kCGLineCapButt,

kCGLineCapRound,

kCGLineCapSquare

};

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

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

typedef CF_ENUM(int32_t, CGLineJoin) {

kCGLineJoinMiter,

kCGLineJoinRound,

kCGLineJoinBevel

};

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

画圆

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

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

- (void)drawCiclePath {

// 传的是正方形,因此就可以绘制出圆了

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.width - 40)];

// 设置填充颜色

UIColor *fillColor = [UIColor greenColor];

[fillColor set];

[path fill];

// 设置画笔颜色

UIColor *strokeColor = [UIColor blueColor];

[strokeColor set];

// 根据我们设置的各个点连线

[path stroke];

}

画椭圆

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

// 画椭圆

- (void)drawOvalPath {

// 传的是不是正方形,因此就可以绘制出椭圆圆了

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, self.frame.size.width - 80, self.frame.size.height - 40)];

// 设置填充颜色

UIColor *fillColor = [UIColor greenColor];

[fillColor set];

[path fill];

// 设置画笔颜色

UIColor *strokeColor = [UIColor blueColor];

[strokeColor set];

// 根据我们设置的各个点连线

[path stroke];

}

画带圆角的矩形

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect

cornerRadius:(CGFloat)cornerRadius;

+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect

byRoundingCorners:(UIRectCorner)corners

cornerRadii:(CGSize)cornerRadii;

第一个工厂方法是画矩形,但是这个矩形是可以画圆角的。第一个参数是矩形,第二个参数是圆角大小。

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

- (void)drawRoundedRectPath {

#if 0

//四个都是圆角10

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.height - 40) cornerRadius:10];

#else

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

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

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

#endif

// 设置填充颜色

UIColor *fillColor = [UIColor greenColor];

[fillColor set];

[path fill];

// 设置画笔颜色

UIColor *strokeColor = [UIColor blueColor];

[strokeColor set];

// 根据我们设置的各个点连线

[path stroke];

}

画弧

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

image

#define kDegreesToRadians(degrees) ((pi * degrees)/ 180)

- (void)drawARCPath {

const CGFloat pi = 3.14159265359;

CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center

radius:100

startAngle:0

endAngle:kDegreesToRadians(135)

clockwise:YES];

path.lineCapStyle = kCGLineCapRound;

path.lineJoinStyle = kCGLineJoinRound;

path.lineWidth = 5.0;

UIColor *strokeColor = [UIColor redColor];

[strokeColor set];

[path stroke];

}

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

这两者正好是相反的。

画二次贝塞尔曲线

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

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

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

endPoint:终端点

controlPoint:控制点,对于二次贝塞尔曲线,只有一个控制点

- (void)drawSecondBezierPath {

UIBezierPath *path = [UIBezierPath bezierPath];

// 首先设置一个起始点

[path moveToPoint:CGPointMake(20, self.frame.size.height - 100)];

// 添加二次曲线

[path addQuadCurveToPoint:CGPointMake(self.frame.size.width - 20, self.frame.size.height - 100)

controlPoint:CGPointMake(self.frame.size.width / 2, 0)];

path.lineCapStyle = kCGLineCapRound;

path.lineJoinStyle = kCGLineJoinRound;

path.lineWidth = 5.0;

UIColor *strokeColor = [UIColor redColor];

[strokeColor set];

[path stroke];

}

画三次贝塞尔曲线

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

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

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

- (void)addCurveToPoint:(CGPoint)endPoint

controlPoint1:(CGPoint)controlPoint1

controlPoint2:(CGPoint)controlPoint2

- (void)drawThirdBezierPath {

UIBezierPath *path = [UIBezierPath bezierPath];

// 设置起始端点

[path moveToPoint:CGPointMake(20, 150)];

[path addCurveToPoint:CGPointMake(300, 150)

controlPoint1:CGPointMake(160, 0)

controlPoint2:CGPointMake(160, 250)];

path.lineCapStyle = kCGLineCapRound;

path.lineJoinStyle = kCGLineJoinRound;

path.lineWidth = 5.0;

UIColor *strokeColor = [UIColor redColor];

[strokeColor set];

[path stroke];

}

我们需要注意,这里确定的起始端点为(20,150),终止端点为(300, 150),基水平方向是一致的。控制点1的坐标是(160,0),水平方向相当于在中间附近,这个参数可以调整。

控制点2的坐标是(160,250),如 果以两个端点的连线为水平线,那么就是250-150=100,也就是在水平线下100。这样看起来就像一个sin函数了。

cic曲线是什么_贝塞尔曲线基本用法相关推荐

  1. css贝塞尔曲线 多个点_贝塞尔曲线实践

    贝塞尔曲线: 贝塞尔曲线本质上是由线段和节点组成的,形象的说节点是可拖动的支点,线段像可伸缩的皮筋.一个常规的曲线往往由4个控制点构成(p0,p1,p2,p3),曲线经过起点(p0)和终点(p1). ...

  2. java贝塞尔曲线_贝塞尔曲线学习

    贝塞尔曲线学习 1.贝塞尔曲线 以下公式中: B(t)为t时间下 点的坐标: P0为起点,Pn为终点,Pi为控制点 一阶贝塞尔曲线(线段): 一阶贝塞尔曲线公式 一阶贝塞尔曲线演示 意义:由 P0 至 ...

  3. java 贝塞尔曲线_贝塞尔曲线:原理、自定义贝塞尔曲线View、使用!!!

    一.原理 转自:http://www.2cto.com/kf/201401/275838.html Android动画学习Demo(3) 沿着贝塞尔曲线移动的Property Animation Pr ...

  4. 贝塞尔曲线 三维 拼接 matlab,贝塞尔曲线公式

    给出了用 2m-1 次贝塞尔曲线逼 近 2m 次贝塞尔曲线的封闭的计算公式,推广了文献[1]中给出的降一次逼近时 的误差估计公式,并得到了封闭的形式.为 CAD 系统的...... 第9期 机械设计与 ...

  5. android 贝塞尔曲线_OpenGL 实践之贝塞尔曲线绘制

    说到贝塞尔曲线,大家肯定都不陌生,网上有很多关于介绍和理解贝塞尔曲线的优秀文章和动态图. 以下两个是比较经典的动图了. 二阶贝塞尔曲线: 三阶贝塞尔曲线: 由于在工作中经常要和贝塞尔曲线打交道,所以简 ...

  6. 【HTML 中的二次贝塞尔曲线 和三次贝塞尔曲线】(使用说明详解)

    二次 贝塞尔曲线 和三次 贝塞尔曲线 使用说明 1. 二次 贝塞尔曲线 和三次 贝塞尔曲线 1.1 贝塞尔曲线的 基本知识 1.2 贝塞尔曲线 生成动图 1.3 HTML 中 画贝塞尔曲线的 2 种方 ...

  7. 二次贝塞尔曲线转换为三次贝塞尔曲线

    二次贝塞尔曲线转换为三次贝塞尔曲线 在使用cairo绘图的时候,发现cairo不支持二次贝塞尔曲线的绘制,为了与QT实现的canvas的行为一致,cairo必须同样实现二次贝塞尔曲线的绘制.思路是将二 ...

  8. ios 贝塞尔曲线 颜色填充_iOS,贝塞尔曲线(UIBezierPath)

    UIBezierPath简介 使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装, UIBezierPath对象是C ...

  9. html5贝塞尔曲线,用canvas绘制一个曲线动画——深入理解贝塞尔曲线

    前言 在前端开发中,贝赛尔曲线无处不在: 它可以用来绘制曲线,在svg和canvas中,原生提供的曲线绘制都是使用贝赛尔曲线 它也可以用来描述一个缓动算法,设置css的transition-timin ...

最新文章

  1. 5G 信令流程 — 5GC 的会话管理(SM,Session Management)
  2. 最新技术资讯,你必须知道的Python 3.9新功能
  3. 抢先看!Kubernetes v1.21 新特性一览
  4. 远程桌面端口修改批处理
  5. 【Flink】Flink Heartbeat of TaskManager with id timed out.
  6. Android开发笔记(一百五十二)H5通过WebView上传图片
  7. POI3.8解决导出大数据量excel文件时内存溢出的问题
  8. 用Promise实现队列(爬一爬慕课网HTML代码)
  9. [CQOI2015]选数
  10. mysql的体系架构和存储引擎
  11. 脑机接口五大应用场景
  12. GB:香港城市大学孙燕妮组发表高准确度病毒株识别工具VirStrain
  13. c语言课程设计高校水电费管理系统
  14. Kali Linux 安装搜狗拼音输入法
  15. 专利学习笔记8:解决CPC签名失败问题
  16. escape在sql语句中的作用
  17. 双相障碍快速循环发作的治疗:证据回顾 | 文献述评
  18. 《中学物理教学参考》期刊简介及投稿须知
  19. discuzx update sitekey.php,Discuz如何清除应用中心密码附加工具
  20. AcWing——第55场周赛

热门文章

  1. Java的左移和右移的含义!
  2. 使用OpenCV进行图像全景拼接
  3. 新版vue-cli搭建多页应用2
  4. Linux 下搭建 WordPress 个人站点
  5. sqlite3 解决并发读写冲突的问题
  6. Selenium启动不同浏览器
  7. java自动装箱性能
  8. Crunch Bang(linux)安装Webstorm上手
  9. angular.foreach 格式
  10. HTML5 —— 视频/音频