UIBezierPath类 笔记
使用UIBezierPath类可以创建基于矢量的路径。此类是Core Graphics框架关于path的一个封装。使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状。
了。为了绘制path对象,我们要用到stroke和fill方法。这些方法在current graphic
context下渲染path的line和curve段。
moveToPoint:设置我们想要创建形状的起点。从这点开始,我们可以用方法addLineToPoint:去创建一个形状的线段。我们可以连续的
创建line,每一个line的起点都是先前的终点,终点就是指定的点。
UIBezierPath* aPath = [UIBezierPath bezierPath]; |
// Set the starting point of the shape. |
[aPath moveToPoint:CGPointMake(100.0, 0.0)]; |
// Draw the lines |
[aPath addLineToPoint:CGPointMake(200.0, 40.0)]; |
[aPath addLineToPoint:CGPointMake(160, 140)]; |
[aPath addLineToPoint:CGPointMake(40.0, 140)]; |
[aPath addLineToPoint:CGPointMake(0.0, 40.0)]; |
[aPath closePath]; |
bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise: 的参数定义了我们想要的arc的圆,以及arc的起点和终点。
Creating a new arc path
// pi is approximately equal to 3.14159265359 |
#define DEGREES_TO_RADIANS(degrees) ((pi * degrees)/ 180) |
- (UIBezierPath*)createArcPath |
{
|
UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) |
radius:75 |
startAngle:0 |
endAngle:DEGREES_TO_RADIANS(135) |
clockwise:YES]; |
return aPath; |
} |

Cubic curve:
addCurveToPoint:controlPoint1:controlPoint2:Quadratic curve:
addQuadCurveToPoint:controlPoint:
bezierPathWithRect: and bezierPathWithOvalInRect:方法去创建椭圆或者矩形形状的path。这两个方法都创建了一个新的path对象,并用指定的形状去初始化它们。我们可以使用返回的path对象或者根据需要去添加更多的形状。Graphics的
CGPathAddEllipseInRect 方法非常的简单使用,也更准确。们可以用UIBezierPath类的方法去添加直线段和曲线段,UIBezierPath类还提供了一个属性CGPath,我们可以用来直接修改底层的
path data type。如果我们希望用Core Graphics 框架函数去创建path,则我们要用到此属性。
Graphics函数去修改path,也可以使用Core
Graphics函数和UIBezierPath函数混合去修改。第一种方法在某些方面相对来说比较容易。我们可以创建一个CGPathRef数据类型,
并调用我们需要修改path信息的函数。
// Create the path data |
CGMutablePathRef cgPath = CGPathCreateMutable(); |
CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(0, 0, 300, 300)); |
CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(50, 50, 200, 200)); |
// Now create the UIBezierPath object |
UIBezierPath* aPath = [UIBezierPath bezierPath]; |
aPath.CGPath = cgPath; |
aPath.usesEvenOddFillRule = YES; |
// After assigning it to the UIBezierPath object, you can release |
// your CGPathRef data type safely. |
CGPathRelease(cgPath); |
Mixing Core Graphics and UIBezierPath calls
UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 300, 300)]; |
// Get the CGPathRef and create a mutable version. |
CGPathRef cgPath = aPath.CGPath; |
CGMutablePathRef mutablePath = CGPathCreateMutableCopy(cgPath); |
// Modify the path and assign it back to the UIBezierPath object |
CGPathAddEllipseInRect(mutablePath, NULL, CGRectMake(50, 50, 200, 200)); |
aPath.CGPath = mutablePath; |
// Release both the mutable copy of the path. |
CGPathRelease(mutablePath); |
state。
Drawing a path in a view
- (void)drawRect:(CGRect)rect |
{
|
// Create an oval shape to draw. |
UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect: |
CGRectMake(0, 0, 200, 100)]; |
// Set the render colors |
[[UIColor blackColor] setStroke]; |
[[UIColor redColor] setFill]; |
CGContextRef aRef = UIGraphicsGetCurrentContext(); |
// If you have content to draw after the shape, |
// save the current state before changing the transform |
//CGContextSaveGState(aRef); |
// Adjust the view's origin temporarily. The oval is |
// now drawn relative to the new origin point. |
CGContextTranslateCTM(aRef, 50, 50); |
// Adjust the drawing options as needed. |
aPath.lineWidth = 5; |
// Fill the path before stroking it so that the fill |
// color does not obscure the stroked line. |
[aPath fill]; |
[aPath stroke]; |
// Restore the graphics state before drawing any other content. |
//CGContextRestoreGState(aRef); |
} |
UIBezierPath通过
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise
可以画出一段弧线。
看下各个参数的意义:
center:圆心的坐标
radius:半径
startAngle:起始的弧度
endAngle:圆弧结束的弧度
clockwise:YES为顺时针,No为逆时针
方法里面主要是理解startAngle与endAngle,刚开始我搞不清楚一段圆弧从哪算起始和终止,比如弧度为0的话,是从上下左右哪个点开始算
看了下面这张图就明了了
看出0Pi就是指圆最右边开始计算的,顺时针依次为M_PI/2,M_PI,M_PI*1.5
明白这个,用BezierPath画圆弧就简单了
比如要画上图加粗的那段就是:
- UIBezierPath *path = [[UIBezierPath alloc] init];
- [path addArcWithCenter:center
- radius:radius
- startAngle:M_PI*1.1
- endAngle:M_PI*1.9
- clockwise:YES];
1.报错名称 : CGContextSaveGState: invalid context 0x0.
CGContextDrawPath: invalid context
0x0.等。我这里编译时并没有报错也无警告,直到运行时才在控制台打印了一系列的<Error>信息 如下图(错误信息)错误信息出现这类错误的原因是没有在 view 的 -drawRect: 方法中对 layer 进行了不适当的操作 例如 UIBezierPath
的 - (void)setFill; - (void)setStroke; 方法 UIColor 的 - (void)setFill; -
(void)setStroke;方法 等。2.解决方法
(1).把有关于 view layer 层的操作单独封装到一个 view 类的 -drawRect: 方法中
(2).不要调用 相关的方法 ,官方一般都给了注释,com+单击 就可查看:如下图(官方给定的限制->drawing context)
- 解决完上面的bug,现在继续学习
- []
UIBezierPath类 笔记的更多相关文章
- iOS 使用UIBezierPath类实现随手画画板
在上一篇文章中我介绍了 UIBezierPath类 介绍 ,下面这篇文章介绍一下如何通过这个类实现一个简单的随手画画板的简单程序demo,功能包括:划线(可以调整线条粗细,颜色),撤销笔画,回撤笔画, ...
- iOS UIBezierPath类 介绍
使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和 ...
- UIBezierPath 类的使用
使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线 ...
- iOS动画之iOS UIBezierPath类 介绍
感谢:http://blog.csdn.net/crayondeng/article/details/11093689 使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类 ...
- ASP运行流程(主要的类笔记)
个人笔记:参考汤姆大叔的MVC之前那些事系列整理 client端发送页面请求,被IIS的某个进程截获,它根据申请的页面后缀(.aspx)不同,调用不同的页面处理程序(.asp->asp.dll ...
- java动态加载类和静态加载类笔记
JAVA中的静态加载类是编译时刻加载类 动态加载类指的是运行时刻加载类 二者有什么区别呢 举一个例子 现在我创建了一个类 实现的功能假设为通过传入的参数调用具体的类和方法 class offic ...
- String类笔记
首先要知道,String类的核心是一个数组 我们所写的字符串序列都会放到这个char数组中,且前面有final修饰,所以只能赋值一次. 所以String创建的是不可变字符串序列,不可修改.如果要对其进 ...
- ES6 的class类 笔记
class Person{ // 构造 constructor(x,y){ this.x = x; this.y = y; } toString(){ return (this.x + "的 ...
- Java常用类笔记(学习尚硅谷java基础教程)
一.Java根类Object类1.toString()方法 1)以文本对象返回,故toString()的定义为public String toString() {} 2)默认的字符串输出是:包.类名@ ...
随机推荐
- HowTo:使用数据流读写消息
本文主要演示使用TPL 数据流库从数据流块(dataflow block)读写消息. 提供了同步方法和异步方法. 主要使用BufferBlock,其既能作为message source,有能作为m ...
- WPF Datagrid multiple selecteditems in MVVM
I can assure you: SelectedItems is indeed bindable as a XAML CommandParameter After a lot of digging ...
- BZOJ 1086 & 类树的分块
题意: “余”人国的国王想重新编制他的 国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理.他的国家有n个城市,编号为1..n.一些城市之间有道路相连,任意两个 不同的城市之间 ...
- JS正则获取参数值
var geturl = function(url){ var ret = {}; var queryStr=url.replace(/^[^\?#]*\??/g,'').replace(/#DIAL ...
- js 图片处理 Jcrop.js API
引入jquery.Jcrop.min.css和jquery.Jcrop.min.js 参数/接口说明 options 参数说明 名称 默认值 说明 allowSelect true 允许新选框 all ...
- .NET易忘备留 ORACLE存储过程调用
1.Oracle存储过程调用[返回信息,单体或者列表] public IResult FundBuild(string partnerId,string userId, DateTime beginD ...
- JSP内置对象---request和 response
<%@page import="java.net.URLEncoder"%> <%@page import="com.hanqi.web.CardDAO ...
- android-对话框
一.常用对话框 AlertDialog: 功能最丰富,实际应用最广的对话框(以下三种对话框都是该对话框的子类) ProgressDialog:进度对话框.这个对话框只是对进度条的包装 DatePick ...
- JAVA双向链表
1.链表是一种重要的数据结构,在程序设计中占有很重要的地位 2.我们可以用类List来实现链表结构,用变量Head.Tail.Length.Pointer来实现表头.存储当前结点的指针时有一定的技 巧 ...
- 一段能导致火狐、谷歌Safari浏览器崩溃,甚至让iPhone重启的代码
JavaScript代码,能导致火狐.谷歌Safari浏览器崩溃,甚至让iPhone重启 <html> <body> <script> var total = &q ...