简介

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

分析

首先我们先看一下,UIBezierPath有哪些重要的属性:
1、 [color set]设置颜色,color为创建的UIColor对象
2、 [path stroke]填充view的线条的颜色,与[color set]配合使用 ,
3、 [path fill]填充整个view内部的颜色,与[color set]配合使用。
4、 path.lineWidth = 5.0; 这个很好理解了,就是划线的宽度
5、 path.lineCapStyle 这个线段起点是终点的样式,这个样式有三种:

  • kCGLineCapButt
  • kCGLineCapRound
  • kCGLineCapSquare

6、 path.lineJoinStyle 这个属性是用来设置两条线连结点的样式,同样它也有三种样式供我们选择

  • kCGLineJoinMiter 直接连接
  • kCGLineJoinRound 圆滑衔接
  • kCGLineJoinBevel 斜角连接

使用

接下来,我们就看一下UIBezierPath到底应该怎么使用:
首先,我们先自定义一个UIView的子类,然后重写- (void)drawRect:(CGRect)rect 方法,将创建图形的方法写到该方法中,下面是一些简单的示例:

画多边形
    UIBezierPath* aPath = [UIBezierPath bezierPath];
aPath.lineWidth = 15.0;
/*
kCGLineCapButt,
kCGLineCapRound,
kCGLineCapSquare
*/
aPath.lineCapStyle = kCGLineCapButt ; //终点(起点)样式
/*
kCGLineJoinMiter,
kCGLineJoinRound,
kCGLineJoinBevel
*/
aPath.lineJoinStyle = kCGLineJoinBevel; //拐点样式 [aPath moveToPoint:CGPointMake(150, 30)];//设置起始点
[aPath addLineToPoint:CGPointMake(250, 70)];//途经点
[aPath addLineToPoint:CGPointMake(210, 170)];//途经点
[aPath addLineToPoint:CGPointMake(90, 170)];//途经点
[aPath addLineToPoint:CGPointMake(50, 70)];//途经点 [aPath closePath];//通过调用closePath方法得到最后一条线 UIColor *strokeColor = [UIColor redColor];
[strokeColor set];
[aPath stroke];//设置线条颜色
UIColor *fillColor = [UIColor blueColor];
[fillColor set];
[aPath fill];//填充

多边形.png

如果是创建四边形可直接使用:

    UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];
画圆
    UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)];
aPath.lineWidth = 5.0;
aPath.lineCapStyle = kCGLineCapRound; //线条拐角
aPath.lineJoinStyle = kCGLineCapRound; //终点处理
UIColor *color = [UIColor redColor];
[color set];
[aPath stroke];

弧形.png

如果要画正圆,将rect的width和height设置为相等的值即可。

画弧形
    /*
ArcCenter: 原点
radius: 半径
startAngle: 开始角度
endAngle: 结束角度
clockwise: 是否是顺时针方向
*/
UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 300)
radius:80
startAngle:0
endAngle:pi
clockwise:NO];
aPath.lineWidth = 5.0;
aPath.lineCapStyle = kCGLineCapRound; //线条拐角
aPath.lineJoinStyle = kCGLineCapRound; //终点处理 UIColor *color = [UIColor redColor];
[color set]; //设置线条颜色
[aPath stroke];

圆形.png
画二次曲线
    UIBezierPath* aPath = [UIBezierPath bezierPath];
aPath.lineWidth = 5.0;
aPath.lineCapStyle = kCGLineCapRound; //线条拐角
aPath.lineJoinStyle = kCGLineCapRound; //终点处理
[aPath moveToPoint:CGPointMake(100, 100)];//设置初始点
//终点 controlPoint:切点(并不是拐弯处的高度,不懂的同学可以去看三角函数)
[aPath addQuadCurveToPoint:CGPointMake(200, 100) controlPoint:CGPointMake(150, 50)]; UIColor *color = [UIColor redColor];
[color set];
[aPath stroke];

二次曲线.png
画三次曲线
    UIBezierPath *path2 = [UIBezierPath bezierPath];
path2.lineWidth = 5.0;
path2.lineCapStyle = kCGLineCapRound;
path2.lineJoinStyle = kCGLineJoinRound;
[path2 moveToPoint:CGPointMake(0, 100)];
[path2 addCurveToPoint:CGPointMake(100, 100) controlPoint1:CGPointMake(25, 50) controlPoint2:CGPointMake(75, 150)];//两个切点
UIColor *color = [UIColor redColor];
[color set];
[path2 stroke];

三次曲线.png

以上便是iOS中UIBezierPath最基本的使用方法了,在平时的开发中,我们经常将UIBezierPath与CALayer配合使用,下面是一个简单的例子:

    //创建CAShapeLayer对象
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = CGRectMake(100, 100, 200, 200);//设置shapeLayer的尺寸和位置
shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色为ClearColor
//设置线条的宽度和颜色
shapeLayer.lineWidth = 1.0f;
shapeLayer.strokeColor = [UIColor redColor].CGColor;
//创建一个圆形贝塞尔曲线
UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
//将贝塞尔曲线设置为CAShapeLayer的path
shapeLayer.path = aPath.CGPath;
//将shapeLayer添加到视图的layer上
[self.view.layer addSublayer:shapeLayer];

shapeLayer.png

总结:

到此为止,关于UIBezierPath最基本的使用就介绍完了,但是关于UIBezierPath在iOS中还有很多更加神奇的应用,有兴趣的同学可以研究一下。

作者:沐泽sunshine
链接:http://www.jianshu.com/p/d7671ec6fbb7
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

iOS贝塞尔曲线(UIBezierPath)的基本使用方法的更多相关文章

  1. iOS开发 贝塞尔曲线UIBezierPath(2)

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

  2. iOS开发 贝塞尔曲线UIBezierPath(后记)

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

  3. 贝塞尔曲线UIBezierPath简单使用

    //常用属性 /* 1.CGPath: 将UIBezierPath类转换成CGPath 2.currentPoint: 当前path的位置,可以理解为path的终点 3.lineWidth: 线条宽度 ...

  4. iOS开发 贝塞尔曲线UIBezierPath

    最近项目中需要用到用贝塞尔曲线去绘制路径 ,然后往路径里面填充图片,找到这篇文章挺好,记录下来 自己学习! 转至 http://blog.csdn.net/guo_hongjun1611/articl ...

  5. UIBezierPath IOS贝塞尔曲线

    //记录  贝塞尔曲线使用 //根据一个矩形画曲线 + (UIBezierPath *)bezierPathWithRect:(CGRect)rect //根据矩形框的内切圆画曲线 + (UIBezi ...

  6. IOS贝塞尔曲线圆形进度条和加载动画

    做项目让做一个加载动画,一个圈圈在转中间加一个图片,网上有好多demo,这里我也自己写了一个,中间的图片可加可不加.其中主要用到贝塞尔曲线.UIBezierPath是对CGContextRef的进一步 ...

  7. IOS 贝塞尔曲线切割圆角

    写一个UIView扩展 1. .h文件 @interface UIView (Corner) - (void)setCornerWithType:(UIRectCorner)type Radius:( ...

  8. iOS - 贝塞尔曲线,折线,曲线,波浪线

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZHlsYW5fbHdiXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  9. iOS开发 贝塞尔曲线

    iOS开发 贝塞尔曲线UIBezierPath - 陌云 时间 2014-03-14 11:04:00  博客园-所有随笔区 原文  http://www.cnblogs.com/moyunmo/p/ ...

随机推荐

  1. Win10下Clion配置opencv3

    本人不想在爱机装一个vs2013或者vs2015这种庞然大物,可是手头要弄一个基于windows的opencv项目,就只好装了个Clion,期间踩了不少坑,记录一下. 参考网址:http://www. ...

  2. Swift 扩展

    前言 扩展就是给一个现存类.结构体.枚举或者协议添加新的属性或者方法,无需修改目标的源代码,就可以把想要的代码加到目标上面. 扩展可以用来扩展现有类型的计算属性.构造器.方法和下标. 不能添加一个已经 ...

  3. 页面生命周期里面还有很东西,如PageHandlerFactory等等这些东东也够吃一壶的,发现每走到一个领域,发现要学的东西实在是太多太多啦,总感觉自己所学的东西只是沧海一粟,走过了这道坎,又是一片海洋,我只能呐喊:生命永不止息,学海无涯----够用就好。

    页面生命周期里面还有很东西,如PageHandlerFactory等等这些东东也够吃一壶的,发现每走到一个领域,发现要学的东西实在是太多太多啦,总感觉自己所学的东西只是沧海一粟,走过了这道坎,又是一片 ...

  4. FCEUX金手指加强版 - 使用Lua脚本语言编写FC/NES金手指脚本

    一直觉得大部分的FC/NES模拟器的作弊码金手指不是那么方便使用, 比如魂斗罗1代, 玩家的武器可以通过修改0xAA的值来改变: 0x11为M弹(重机枪),0x12为F弹(圈圈),0x13为S弹(散弹 ...

  5. Easyui 中 Tabsr的常用方法

    注:index必须为变量 tab页从0开始 //新增一个tab页var index = 0;$('#tt').tabs('add',{ title: 'Tab'+index, content: '&l ...

  6. SLA等级那些9的实际意义

    1. 重要的系统起码要设计达到99.9%的可靠性吧. 俗称3个9,这是什么意思呢? (1-99.9%)*365*24=8.76小时,表示该软件系统在连续运行1年时间里最多可能的业务中断时间是8.76小 ...

  7. U811.1接口EAI系列之一--通用把XML传送给EAI处理方法--PowerBuilder语言

    1.前面配置参考:http://www.cnblogs.com/spring_wang/p/3393147.html 2.pb通用调EAI方法代码如下: //===================== ...

  8. 解决ssh连接慢(有时候等半分钟才出现密码输入提示)的方法

    经常通过ssh 或者 scp 连接一堆远程主机,同样是 Linux 主机,其中一些创建 ssh 连接速度特别慢,连接建立之后执行操作速度却很正常,看来应该不是网络原因.解决的方法是通过ssh 的-v参 ...

  9. Ambari修改主页面方法

    [root@hdp159 ambari-web]# brunch watch --server Oct :: - info: application started on http://localho ...

  10. Android studio界面相关设置

    用惯了emacs的操作方式,每当使用一款新的编辑器的时候,第一个想到的就是这个工具有没有emacs的快捷键,Android studio也是一样的. 1. Android studio设置emacs的 ...