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

使用方法

UIBezierPath 是对 CGPathRef 的封装。创建矢量图形时,拆解成一或多条线段,拼接起来,每条线段的终点都是下一条线段的起点。

具体地:

1.创建一个 UIBezierPath 对象

2.用 moveToPoint: 设置初始线段的起点

3.添加线段,定义一或多个子路径

4.修改 UIBezierPath 的绘图相关的属性,比如stroke path的属性 lineWidth 和 lineJoinStyle ,filled path的属性 usesEvenOddFillRule

注意:如果是矩形或者圆之类的特殊图形,可以不用第2步。

代码案例

  • 画直线
    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(100, 50)]; path.lineWidth = 5.0f;
path.lineJoinStyle = kCGLineJoinRound; [path stroke];

  • 创建三角形
    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(300, 50)];
[path addLineToPoint:CGPointMake(200, 150)]; // 最后的闭合线是可以通过调用closePath方法来自动生成的,也可以调用-addLineToPoint:方法来添加
// [path addLineToPoint:CGPointMake(50, 50)]; [path closePath]; path.lineWidth = 5.0f; [path stroke];

  • 创建矩形
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 100, 50, 50)];

    path.lineWidth = 5.0f;

    [path stroke];

  • 创建内切曲线
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 200, 50, 50)];

    path.lineWidth = 5.0f;

    [path stroke];

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 200, 50, 100)];

    path.lineWidth = 5.0f;

    [path stroke];

  • 创建带有圆角的矩形,当矩形变成正圆的时候,Radius就不再起作用
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 300, 50, 50) cornerRadius:15.0f];

    path.lineWidth = 5.0f;

    [path stroke];

  • 设定特定的角为圆角的矩形
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 400, 50, 50) byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5,5)];

    path.lineWidth = 5.0f;

    [path stroke];

  • 创建圆弧
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 550) radius:25 startAngle:0 endAngle:1.5*M_PI clockwise:YES];

    path.lineWidth = 5.0f;

    [path stroke];

  • 通过路径A创建路径B
    UIBezierPath *path_A = [UIBezierPath bezierPath];

    [path_A moveToPoint:CGPointMake(200, 50)];

    [path_A addLineToPoint:CGPointMake(250, 100)];

    path_A.lineWidth = 5.0f;

    UIBezierPath *path_B = [UIBezierPath bezierPathWithCGPath:path_A.CGPath];

    [path_B stroke];

  • 创建三次贝塞尔曲线
    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(100, 200)];

    [path addCurveToPoint:CGPointMake(300, 200) controlPoint1:CGPointMake(150, 150) controlPoint2:CGPointMake(250, 250)];

    [path stroke];

  • 创建二次贝塞尔曲线
    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(100, 200)];

    [path addQuadCurveToPoint:CGPointMake(300, 200) controlPoint:CGPointMake(150, 150)];

    [path stroke];

  • 添加圆弧
    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(200, 400)];
[path addLineToPoint:CGPointMake(225, 410)]; [path addArcWithCenter:CGPointMake(200, 400) radius:25 startAngle:0 endAngle:1.5*M_PI clockwise:YES]; // [path closePath];
// [path removeAllPoints]; [path stroke];

  • 追加路径
    UIBezierPath *path_A = [UIBezierPath bezierPath];

    [path_A moveToPoint:CGPointMake(200, 500)];
[path_A addLineToPoint:CGPointMake(225, 410)]; UIBezierPath *path_B = [UIBezierPath bezierPath]; [path_B moveToPoint:CGPointMake(200, 600)];
[path_B addLineToPoint:CGPointMake(225, 500)]; [path_A appendPath:path_B]; [path_A stroke];

  • 创建翻转路径,即起点变成终点,终点变成起点
    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(100, 50)]; path.lineWidth = 5.0f; NSLog(@"%@",NSStringFromCGPoint(path.currentPoint)); UIBezierPath *path_b = [path bezierPathByReversingPath];
CGAffineTransform transform = CGAffineTransformMakeTranslation(200, 0);
[path_b applyTransform: transform];
// 两条路径分别添加一条直接到 self.center
[path addLineToPoint: CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5)];
[path_b addLineToPoint: CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5)]; NSLog(@"%@",NSStringFromCGPoint(path_b.currentPoint));
[[UIColor redColor] set];
[path stroke];
[[UIColor blueColor] set];
[path_b stroke];

  • 路径进行仿射变换
    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(100, 50)];
[path addLineToPoint:CGPointMake(200, 50)]; CGAffineTransform transform = CGAffineTransformRotate(self.transform, M_PI_4);
[path applyTransform:transform]; path.lineWidth = 5.0f; [path stroke];

  • 创建虚线
    CGFloat dashStyle[] = {1.0f, 2.0f};

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(100, 50)]; [path setLineDash:dashStyle count:2 phase:0.0]; [path stroke];

  • 设置颜色
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 100)];

    [[UIColor greenColor] setStroke];

    [[UIColor redColor] setFill];

    [path stroke];
[path fill];

  • 设置描边混合模式
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 100)];

    [[UIColor greenColor] setStroke];

    path.lineWidth = 10.0f;

    [path strokeWithBlendMode:kCGBlendModeSaturation alpha:1.0];

    [path stroke];

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 100)];

    [[UIColor redColor] setFill];

    [path fillWithBlendMode:kCGBlendModeSaturation alpha:0.6];

    [path fill];

  • 修改当前图形上下文的绘图区域可见,随后的绘图操作导致呈现内容只有发生在指定路径的填充区域
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 100)];

    [[UIColor greenColor] setStroke];

    [path addClip];

    [path stroke];

结语

关于UIBezierPath的简单介绍就到这了,主要是用代码做了展示,属性跟方法,没详细去介绍,我觉得可以直接看苹果的api写的也蛮清楚的.或者自己试试不同的参数样式也能大概理解了.另外简书上看到有篇文章对属性及方法介绍的蛮详细的,可以去看看:UIBezierPath

核心动画跟贝赛尔曲线都有了简单的介绍了,接下来就可以动手做点简单的自定义动画了.

快速上手UIBezierPath的更多相关文章

  1. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

  2. 快速上手Unity原生Json库

    现在新版的Unity(印象中是从5.3开始)已经提供了原生的Json库,以前一直使用LitJson,研究了一下Unity用的JsonUtility工具类的使用,发现使用还挺方便的,所以打算把项目中的J ...

  3. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen快速上手

    原文链接:Hello, Android Multiscreen Quickstart. 译文链接:Hello,Android Multiscreen快速上手 本部分介绍利用Xamarin.Androi ...

  4. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  5. 快速上手seajs——简单易用Seajs

    快速上手seajs——简单易用Seajs   原文  http://www.cnblogs.com/xjchenhao/p/4021775.html 主题 SeaJS 简易手册 http://yslo ...

  6. Git版本控制Windows版快速上手

    说到版本控制,之前用过VSS,SVN,Git接触不久,感觉用着还行.写篇博文给大家分享一下使用Git的小经验,让大家对Git快速上手. 说白了Git就是一个控制版本的工具,其实没想象中的那么复杂,咱在 ...

  7. Objective-C快速上手

    最近在开发iOS程序,这篇博文的内容是刚学习Objective-C时做的笔记,力图达到用最短的时间了解OC并使用OC.Objective-C是OS X 和 iOS平台上面的主要编程语言,它是C语言的超 ...

  8. Netron开发快速上手(二):Netron序列化

    Netron是一个C#开源图形库,可以帮助开发人员开发出类似Visio的作图软件.本文继前文”Netron开发快速上手(一)“讨论如何利用Netron里的序列化功能快速保存自己开发的图形对象. 一个用 ...

  9. Netron开发快速上手(一):GraphControl,Shape,Connector和Connection

    版权所有,引用请注明出处:<<http://www.cnblogs.com/dragon/p/5203663.html >> 本文所用示例下载FlowChart.zip 一个用 ...

随机推荐

  1. Mongoose与bluebird结合使用实例

    nodejs的所有调用几乎是全异步的,而所有的IO操作也都是通过回调函数才能知道结果. 如果一个异步调用依赖另一个异步调用,如果没有Promise的话,有可能陷入传说中的回调地狱. bluebird实 ...

  2. Mac下安装cscope和ctags

    Mac下默认没有cscope和ctags,太不爽了,还好可以自己编译一个放进来 一.下载地址 cscope: http://downloads.sourceforge.net/project/csco ...

  3. 遍历ul下的li,点击弹出li的索引

    首先我们需要一个html结构 <div > <ul> <li>a</li> <li>a</li> <li>a< ...

  4. Oracle执行计划顺序

    先从最开头一直往右看,直到看到最右边的并列的地方,对于不并列的,靠右的先执行:对于并列的,靠上的先执行.      即并列的缩进块,从上往下执行,非并列的缩进块,从下往上执行. 如下示例: Execu ...

  5. jQuery --checkbox全选和取消全选简洁高效的解决办法

    最近在公司做了一个小项目,其中有一个全选和取消全选的这么一个模块,搞了半天找不到一种最佳的解决方案!后来通过各种努力找到了一种简洁高效的解决办法,这里想和大家分享一下.有问题的话,还望各路大神指导一二 ...

  6. (求租仓库)navigationController .navigationBar 的属性设置

    需要做成的效果如下图的

  7. asp.net core mvc剖析:启动流程

    asp.net core mvc是微软开源的跨平台的mvc框架,首先它跟原有的MVC相比,最大的不同就是跨平台,然后又增加了一些非常实用的新功能,比如taghelper,viewcomponent,D ...

  8. 微信小程序image组件binderror使用例子(对应html、js中的onerror)

    官方文档  binderror HandleEvent 当错误发生时,发布到 AppService 的事件名,事件对象event.detail = {errMsg: 'something wrong' ...

  9. Jenkins在windows上的安装配置

     今天是2月14号,所谓西方情人节,下班回来发现,2月14过的比七夕还火热.于是上网百度百科查询了"情人节". 毕竟是中国的百度啊.是这么解释的.我感到很欣慰.过得每一个节日都应该 ...

  10. 基于Ubuntu12.04-server版的openstack F版搭建步骤

    本文为双节点搭建,分为计算(IP1)和控制节点(IP2) 说明: 计算节点组件: 1.mysql 2.keystone 3.Nova 4.glance 5.rabbitmq 控制节点组件: 1.cin ...