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

    
1.Bezier Path 基础
   UIBezierPath对象是CGPathRef数据类型的封装。path如果是基于矢量形状的,都用直线和曲线段去创建。我们使用直线段去创建矩形和多边形,使用曲线段去创建弧(arc),圆或者其他复杂的曲线形状。每一段都包括一个或者多个点,绘图命令定义如何去诠释这些点。每一个直线段或者曲线段的结束的地方是下一个的开始的地方。每一个连接的直线或者曲线段的集合成为subpath。一个UIBezierPath对象定义一个完整的路径包括一个或者多个subpaths。
 
   创建和使用一个path对象的过程是分开的。创建path是第一步,包含一下步骤:
(1)创建一个Bezier path对象。
(2)使用方法moveToPoint:去设置初始线段的起点。
(3)添加line或者curve去定义一个或者多个subpaths。
(4)改变UIBezierPath对象跟绘图相关的属性。
例如,我们可以设置stroked path的属性lineWidth和lineJoinStyle。也可以设置filled path的属性usesEvenOddFillRule。
 
   当创建path,我们应该管理path上面的点相对于原点(0,0),这样我们在随后就可以很容易的移动path了。为了绘制path对象,我们要用到stroke和fill方法。这些方法在current graphic context下渲染path的line和curve段。
 
 
2、使用UIBezierPath创建多边形---在path下面添加直线条形成多边形
多边形是一些简单的形状,这些形状是由一些直线线条组成,我们可以用moveToPoint: 和 addLineToPoint:方法去构建。
方法moveToPoint:设置我们想要创建形状的起点。从这点开始,我们可以用方法addLineToPoint:去创建一个形状的线段。
我们可以连续的创建line,每一个line的起点都是先前的终点,终点就是指定的点。
 
下面的代码描述了如何用线段去创建一个五边形。第五条线通过调用closePath方法得到的,它连接了最后一个点(0,40)和第一个点(100,0)
说明:closePath方法不仅结束一个shape的subpath表述,它也在最后一个点和第一个点之间画一条线段,如果我们画多边形的话,这个一个便利的方法我们不需要去画最后一条线。
 

注:这个类要继承自UIView。

 
运行的结果如下图:
 
如果修改最后一句代码:[aPathfill];
运行结果就如下:
 
这样就知道stroke  和  fill  方法的区别了吧!
 
3、使用UIBezierPath创建矩形
使用这个方法即可:
 Creates and returns a new UIBezierPath object initialized with a rectangular path.  

 + (UIBezierPath *)bezierPathWithRect:(CGRect)rect  

demo代码:

4、使用UIBezierPath创建圆形或者椭圆形

使用这个方法即可:

这个方法根据传入的rect矩形参数绘制一个内切曲线。

当传入的rect是一个正方形时,绘制的图像是一个内切圆;当传入的rect是一个长方形时,绘制的图像是一个内切椭圆。
 
5、使用UIBezierPath创建一段弧线
使用这个方法:
 
 Creates and returns a new UIBezierPath object initialized with an arc of a circle.

 + (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise
Parameters
center
Specifies the center point of the circle (in the current coordinate system) used to define the arc.
radius
Specifies the radius of the circle used to define the arc.
startAngle
Specifies the starting angle of the arc (measured in radians).
endAngle
Specifies the end angle of the arc (measured in radians).
clockwise
The direction in which to draw the arc.
Return Value
A new path object with the specified arc.
其中的参数分别指定:这段圆弧的中心,半径,开始角度,结束角度,是否顺时针方向。
 
下图为弧线的参考系。
 

demo代码:
#define pi 3.14159265359
#define DEGREES_TO_RADIANS(degrees) ((pi * degrees)/ 180)
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor redColor];
[color set]; //设置线条颜色 UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(, )
radius:
startAngle:
endAngle:DEGREES_TO_RADIANS()
clockwise:YES]; aPath.lineWidth = 5.0;
aPath.lineCapStyle = kCGLineCapRound; //线条拐角
aPath.lineJoinStyle = kCGLineCapRound; //终点处理 [aPath stroke];
}
 

结果如下图:

 
6、UIBezierPath类提供了添加二次贝塞尔曲线和三次贝塞尔曲线的支持。
曲线段在当前点开始,在指定的点结束。曲线的形状有开始点,结束点,一个或者多个控制点的切线定义。下图显示了两种曲线类型的相似,以及控制点和curve形状的关系。
 
(1)绘制二次贝塞尔曲线
 
使用到这个方法:
  1. Appends a quadratic Bézier curve to the receiver’s path.
  2. - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
  3. Parameters
  4. endPoint
  5. The end point of the curve.
  6. controlPoint
  7. The control point of the curve.

 
demo代码:

 
 
(2)绘制三次贝塞尔曲线
 
使用到这个方法:
  1. Appends a cubic Bézier curve to the receiver’s path.
  2. - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
  3. Parameters
  4. endPoint
  5. The end point of the curve.
  6. controlPoint1
  7. The first control point to use when computing the curve.
  8. controlPoint2
  9. The second control point to use when computing the curve.

 
demo代码:
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor redColor];
[color set]; //设置线条颜色 UIBezierPath* aPath = [UIBezierPath bezierPath]; aPath.lineWidth = 5.0;
aPath.lineCapStyle = kCGLineCapRound; //线条拐角
aPath.lineJoinStyle = kCGLineCapRound; //终点处理 [aPath moveToPoint:CGPointMake(, )]; [aPath addCurveToPoint:CGPointMake(, ) controlPoint1:CGPointMake(, ) controlPoint2:CGPointMake(, )]; [aPath stroke];
}

 
 
7.使用Core Graphics函数去修改path。
    UIBezierPath类只是CGPathRef数据类型和path绘图属性的一个封装。虽然通常我们可以用UIBezierPath类的方法去添加直线段和曲线段,UIBezierPath类还提供了一个属性CGPath,我们可以用来直接修改底层的path data type。如果我们希望用Core Graphics 框架函数去创建path,则我们要用到此属性。
 
    有两种方法可以用来修改和UIBezierPath对象相关的path。可以完全的使用Core Graphics函数去修改path,也可以使用Core Graphics函数和UIBezierPath函数混合去修改。第一种方法在某些方面相对来说比较容易。我们可以创建一个CGPathRef数据类型,并调用我们需要修改path信息的函数。
下面的代码就是赋值一个新的CGPathRef给UIBezierPath对象。
 // Create the path data
CGMutablePathRef cgPath = CGPathCreateMutable();
CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(, , , ));
CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(, , , )); // 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);
     如果我们使用Core Graphics函数和UIBezierPath函数混合方法,我们必须小心的移动path 信息在两者之间。因为UIBezierPath类拥有自己底层的CGPathRef data type,我们不能简单的检索该类型并直接的修改它。相反,我们应该生成一个副本,然后修改此副本,然后赋值此副本给CGPath属性,如下代码:

Mixing Core Graphics and UIBezierPath calls

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

 // 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(, , , ));
aPath.CGPath = mutablePath; // Release both the mutable copy of the path.
CGPathRelease(mutablePath);
 
8.rendering(渲染)Bezier Path对象的内容。
   当创建一个UIBezierPath对象之后,我们可以使用它的stroke和fill方法在current graphics context中去渲染它。在调用这些方法之前,我们要进行一些其他的任务去确保正确的绘制path。
   使用UIColor类的方法去stroke和fill想要的颜色。
   设置形状在目标视图中的位置。如果我们创建的path相对于原点(0,0),则我们可以给current drawing context应用一个适当的affie transform。例如,我想drawing一个形状起始点在(0,0),我可以调用函数CGContextTranslateCTM,并指定水平和垂直方向的translation值为10。调整graphic context相对于调整path对象的points是首选的方法,因为我们可以很容易的保存和撤销先前的graphics state。
    更新path对象的drawing 属性。当渲染path时,UIBezierPath实例的drawing属性会覆盖graphics context下的属性值。
 
   下面的代码实现了在一个自定义view中实现drawRect:方法中去绘制一个椭圆。椭圆边框矩形的左上角位于视图坐标系统的点(50,50)处。

Drawing a path in a view

- (void)drawRect:(CGRect)rect
{
// Create an oval shape to draw.
UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:
CGRectMake(, , , )]; // 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, , ); // Adjust the drawing options as needed.
aPath.lineWidth = ; // 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);
}
说明:
 
1、这篇文章以上内容部分参考自:http://blog.csdn.net/guo_hongjun1611/article/details/7839371 博客,进行了内容和demo代码补充。
 
2、本人在实现一个小画板功能的时候,发现这个类实现挺不错的,所以这篇博文就对 UIBezierPath 类进行了比较详细的介绍,更加详细的内容请参看文档。

iOS UIBezierPath类 介绍的更多相关文章

  1. iOS动画之iOS UIBezierPath类 介绍

    感谢:http://blog.csdn.net/crayondeng/article/details/11093689 使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类 ...

  2. iOS UIBezierPath知识介绍

    UIBezierPath是在画图,定制动画轨迹中都有应用. UIBezierPath有许多类方法,能够创建基本的曲线,比如利用一个rect创建一个椭圆path的方法:bezierPathWithOva ...

  3. iOS 使用UIBezierPath类实现随手画画板

    在上一篇文章中我介绍了 UIBezierPath类 介绍 ,下面这篇文章介绍一下如何通过这个类实现一个简单的随手画画板的简单程序demo,功能包括:划线(可以调整线条粗细,颜色),撤销笔画,回撤笔画, ...

  4. UIBezierPath 类的使用

    使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线 ...

  5. ios中框架介绍

    ios中框架介绍 参考博客: 参考文章:框架介绍 框架介绍 框架就是一个目录,一个目录包含了共享库,访问共享库里面的代码的头文件,和其他的图片和声音的资源文件.一个共享库定义的方法和函数可以被应用程序 ...

  6. istringstream、ostringstream、stringstream 类介绍 和 stringstream类 clear函数的真正用途

    istringstream.ostringstream.stringstream 类介绍 和 stringstream类 clear函数的真正用途 来源: http://blog.csdn.net/T ...

  7. TynSerial类介绍

    TynSerial类介绍 TynSerial是咏南中间件封装的,支持数据二进制序列(还原)的类. 支持WINDOWS.LINUX.MAC.IOS.ANDROID. 支持D6及以上版本. 支持TCP/H ...

  8. CYQ.Data.Orm.DBFast 新增类介绍(含类的源码及新版本配置工具源码)

    前言: 以下功能在国庆期就完成并提前发布了,但到今天才有时间写文介绍,主要是国庆后还是选择就职了,悲催的是上班的地方全公司都能上网,唯独开发部竟不让上网,是个局域网. 也不是全不能上,房间里有三台能上 ...

  9. IOS 公共类-MyDateUtil 日期处理Util

    IOS 公共类-MyDateUtil 日期处理Util 此为处理日期的公共类.适用IOS6+ .h文件: #import <Foundation/Foundation.h> //适用 IO ...

随机推荐

  1. 创建 Mac OS X 10.9 USB 安装盘

    通过 App Store 下载最新的 OS X 10.9 在“应用程序”目录找到下载的 OS X 10.9 安装文件,选中并鼠标右键,菜单中选择“显示包内容” 弹出的 Finder 中进入 Conte ...

  2. Web缓存解决方案

    缓存是构建于HTTP统一接口之上的最有用功能之一.可以利用缓存减少终端用户感知到的延时,增加可靠性,减少带宽使用和成本,降低服务器负载.缓存无处不在,可以在服务器网络里,内容分发网络(Content ...

  3. Microsoft2013校园招聘笔试题

    Microsoft2013校园招聘笔试题 继续求拍砖!!!! 1. You are managing the database of a book publichser, you currently ...

  4. [分享]CSS美化浏览器滚动条

    今天看到一个站点的滚动条样式特别漂亮,顺便上网搜了一些相关资料,分享给大家: PS:兼容所有浏览器的滚动条样式目前是不存在的. IE下的滚动条样式 IE是最早提供滚动条的样式支持,好多年了,但是其它浏 ...

  5. DEV 打印gridcontrl

    private void PrintPreview(DevExpress.XtraPrinting.IPrintable gridControlPrint)         {             ...

  6. 【C++自我精讲】基础系列四 static

    [C++自我精讲]基础系列四 static 0 前言 变量的存储类型:存储类型按变量的生存期划分,分动态存储方式和静态存储方式. 1)动态存储方式的变量,生存期为变量所在的作用域.即程序运行到此变量时 ...

  7. Android零点一度的区别——Matrix

    2013-07-07 导语:Matrix是android中对图像绘制的处理(旋转.放缩.平移等等),貌似书本翻页就是用这种方式处理的 正文: 1.基于坐标(px,py)旋转degrees度, post ...

  8. sort()方法理解

    转作者:SMTNinja来源:知乎 下面这个排序函数展示了任何基于比较的排序算法 (comparison-based sorting algorithm) 都有的一段代码: 从大到小排序(一列东西, ...

  9. SpringAOP导致@Autowired依赖注入失败

    之前用springAOP做了个操作日志记录,这次在往其他类上使用的时候,service一直注入失败,找了网上好多内容,发现大家都有类似的情况出现,但是又和自己的情况不太符合.后来总结自己的情况发现:方 ...

  10. c语言中细节注意(初级)

    /* 编写如下函数,不使用下标运算符,返回字符串str中字符c的个数 (若不存在则为0). */ #include <stdio.h> int str_chnum(const char * ...