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)默认的字符串输出是:包.类名@ ...
随机推荐
- 获取ItemsControl中当前item的binding数据
直接用 {Binding} 就可以了,如下: <ItemsControl ItemsSource="{Binding Path=ProcessItems}"> < ...
- NUC_HomeWork1 -- POJ1088(DP)
D - 滑雪 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Mic ...
- 【BZOJ1827】[Usaco2010 Mar]gather 奶牛大集会 树形DP
[BZOJ][Usaco2010 Mar]gather 奶牛大集会 Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来 ...
- ajax与HTML5 history pushState/replaceState实例
一.本文就是个实例展示 三点: 我就TM想找个例子,知道如何个使用,使用语法什么的滚粗 跟搜索引擎搞基 自己备忘 精力总是有限的,昨天一冲动,在上海浦东外环之外订了个90米的房子,要借钱筹首付.贷款和 ...
- JAVA的自动装箱拆箱
转自:http://www.cnblogs.com/danne823/archive/2011/04/22/2025332.html 蛋呢 的空间 ??什么是自动装箱拆箱 基本数据类型的自动装箱(a ...
- Codeforces Round #249 (Div. 2) B. Pasha Maximizes
看到题目的时候,以为类似插入排序,比较第i个元素和第i-1个元素, 如果第i个元素比第i-1个元素小,则不交换 如果第i个元素比第i-1个元素大,则交换第i个元素和第i-1个元素 继续比较第i-1个元 ...
- 【HDU】3480 Division
http://acm.hdu.edu.cn/showproblem.php?pid=3480 题意:一个n个元素的集合S要求分成m个子集且子集并为S,要求$\sum_{S_i} (MAX-MIN)^2 ...
- Codeforces Beta Round #7
A题,应该是水题,我没看懂... B题,这题很多坑,注意x是LL,而且x = 0的情况,所以初始化要为-1,最后这题是内存管理啊..操作系统学的不好,题意读不懂啊. 申请内存的时候,是从头找 如果这一 ...
- 转载 Android快捷键 转载
一.实用类快捷键 1 常用熟悉的快捷键 CTRL+C(复制).CTRL+X(剪切).CTRL+Z(撤销).CTRL+F(查找).CTRL+H(搜索文件或字符串).CTRL+Y(重做).CTRL+/(双 ...
- Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)
spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,Charact ...