iOS开发 贝塞尔曲线UIBezierPath - 陌云

iOS开发 贝塞尔曲线UIBezierPath

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

使用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表述,它也在最后一个点和第一个点之间画一条线段,如果我们画多边形的话,这个一个便利的方法我们不需要去画最后一条线。

 

[cpp]    view plain copy

  1. // Only override drawRect: if you perform custom drawing.
  2. - (  void )drawRect:(CGRect)rect
  3. {
  4. UIColor *color = [UIColor redColor];
  5. [color set];   //设置线条颜色
  6. UIBezierPath* aPath = [UIBezierPath bezierPath];
  7. aPath.lineWidth = 5.0;
  8. aPath.lineCapStyle = kCGLineCapRound;   //线条拐角
  9. aPath.lineJoinStyle = kCGLineCapRound;   //终点处理
  10. // Set the starting point of the shape.
  11. [aPath moveToPoint:CGPointMake(100.0, 0.0)];
  12. // Draw the lines
  13. [aPath addLineToPoint:CGPointMake(200.0, 40.0)];
  14. [aPath addLineToPoint:CGPointMake(160, 140)];
  15. [aPath addLineToPoint:CGPointMake(40.0, 140)];
  16. [aPath addLineToPoint:CGPointMake(0.0, 40.0)];
  17. [aPath closePath];  //第五条线通过调用closePath方法得到的
  18. [aPath stroke];  //Draws line 根据坐标点连线
  19. }

注:这个类要继承自UIView。

 

运行的结果如下图:

 
 

如果修改最后一句代码: [aPath fill ];

运行结果就如下:

 
 

这样就知道  stroke  和  fill  方法的区别 了吧!

 

3、使用UIBezierPath创建矩形

使用这个方法即可:

[cpp]    view plain copy

  1. Creates and returns a  new  UIBezierPath object initialized with a rectangular path.
  2. + (UIBezierPath *)bezierPathWithRect:(CGRect)rect

[cpp]    view plain copy

  1. - ( void )drawRect:(CGRect)rect
  2. {
  3. UIColor *color = [UIColor redColor];
  4. [color set];   //设置线条颜色
  5. UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 100, 50)];
  6. aPath.lineWidth = 5.0;
  7. aPath.lineCapStyle = kCGLineCapRound;   //线条拐角
  8. aPath.lineJoinStyle = kCGLineCapRound;   //终点处理
  9. [aPath stroke];
  10. }

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

使用这个方法即可:

[cpp]    view plain copy

  1. Creates and returns a  new  UIBezierPath object initialized with an oval path inscribed in the specified rectangle
  2. + (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect

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

当传入的rect是一个正方形时,绘制的图像是一个内切圆;当传入的rect是一个长方形时,绘制的图像是一个内切椭圆。

 

5、使用UIBezierPath创建一段弧线

使用这个方法:
 

[cpp]    view plain copy

  1. Creates and returns a  new  UIBezierPath object initialized with an arc of a circle.
  2. + (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(  BOOL )clockwise
  3. Parameters
  4. center
  5. Specifies the center point of the circle (in the current coordinate system) used to define the arc.
  6. radius
  7. Specifies the radius of the circle used to define the arc.
  8. startAngle
  9. Specifies the starting angle of the arc (measured in radians).
  10. endAngle
  11. Specifies the end angle of the arc (measured in radians).
  12. clockwise
  13. The direction in which to draw the arc.
  14. Return Value
  15. A   new  path object with the specified arc.
 

其中的参数分别指定:这段圆弧的中心,半径,开始角度,结束角度,是否顺时针方向。

 

下图为弧线的参考系。

 

demo代码:

[cpp]    view plain copy

  1. #define pi 3.14159265359
  2. #define   DEGREES_TO_RADIANS(degrees)  ((pi * degrees)/ 180)
 

[cpp]    view plain copy

  1. - ( void )drawRect:(CGRect)rect
  2. {
  3. UIColor *color = [UIColor redColor];
  4. [color set];   //设置线条颜色
  5. UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
  6. radius:75
  7. startAngle:0
  8. endAngle:DEGREES_TO_RADIANS(135)
  9. clockwise:YES];
  10. aPath.lineWidth = 5.0;
  11. aPath.lineCapStyle = kCGLineCapRound;   //线条拐角
  12. aPath.lineJoinStyle = kCGLineCapRound;   //终点处理
  13. [aPath stroke];
  14. }
 

结果如下图:  

 

6、UIBezierPath类提供了添加二次贝塞尔曲线和三次贝塞尔曲线的支持。

曲线段在当前点开始,在指定的点结束。曲线的形状有开始点,结束点,一个或者多个控制点的切线定义。下图显示了两种曲线类型的相似,以及控制点和curve形状的关系。

 

(1)绘制二次贝塞尔曲线

 

使用到这个方法:

[cpp]    view plain copy

  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代码:

[cpp]    view plain copy

  1. - ( void )drawRect:(CGRect)rect
  2. {
  3. UIColor *color = [UIColor redColor];
  4. [color set];   //设置线条颜色
  5. UIBezierPath* aPath = [UIBezierPath bezierPath];
  6. aPath.lineWidth = 5.0;
  7. aPath.lineCapStyle = kCGLineCapRound;   //线条拐角
  8. aPath.lineJoinStyle = kCGLineCapRound;   //终点处理
  9. [aPath moveToPoint:CGPointMake(20, 100)];
  10. [aPath addQuadCurveToPoint:CGPointMake(120, 100) controlPoint:CGPointMake(70, 0)];
  11. [aPath stroke];
  12. }

 
 

(2)绘制三次贝塞尔曲线

 

使用到这个方法:

[cpp]    view plain copy

  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代码:

[cpp]    view plain copy

  1. - ( void )drawRect:(CGRect)rect
  2. {
  3. UIColor *color = [UIColor redColor];
  4. [color set];   //设置线条颜色
  5. UIBezierPath* aPath = [UIBezierPath bezierPath];
  6. aPath.lineWidth = 5.0;
  7. aPath.lineCapStyle = kCGLineCapRound;   //线条拐角
  8. aPath.lineJoinStyle = kCGLineCapRound;   //终点处理
  9. [aPath moveToPoint:CGPointMake(20, 50)];
  10. [aPath addCurveToPoint:CGPointMake(200, 50) controlPoint1:CGPointMake(110, 0) controlPoint2:CGPointMake(110, 100)];
  11. [aPath stroke];
  12. }

 

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(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);

如果我们使用Core Graphics函数和UIBezierPath函数混合方法,我们必须小心的移动path 信息在两者之间。因为UIBezierPath类拥有自己底层的CGPathRef data type,我们不能简单的检索该类型并直接的修改它。相反,我们应该生成一个副本,然后修改此副本,然后赋值此副本给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);

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(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);
}

iOS开发 贝塞尔曲线的更多相关文章

  1. iOS开发 贝塞尔曲线UIBezierPath

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

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

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

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

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

  4. iOS 使用贝塞尔曲线绘制路径

    使用贝塞尔曲线绘制路径 大多数时候,我们在开发中使用的控件的边框是矩形,或者做一点圆角,是使得矩形的角看起来更加的圆滑. 但是如果我们想要一个不规则的图形怎么办?有人说,叫UI妹子做,不仅省事,还可以 ...

  5. iOS:使用贝塞尔曲线绘制图表(折线图、柱状图、饼状图)

    1.介绍: UIBezierPath :画贝塞尔曲线的path类 UIBezierPath定义 : 贝赛尔曲线的每一个顶点都有两个控制点,用于控制在该顶点两侧的曲线的弧度. 曲线的定义有四个点:起始点 ...

  6. iOS开发之画图板(贝塞尔曲线)

    贝塞尔曲线,听着挺牛气一词,不过下面我们在做画图板的时候就用到贝塞尔绘直线,没用到绘制曲线的功能.如果会点PS的小伙伴会对贝塞尔曲线有更直观的理解.这篇博文的重点不在于如何用使用贝塞尔曲线,而是利用贝 ...

  7. iOS贝塞尔曲线(UIBezierPath)的基本使用方法

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

  8. 【Unity3d游戏开发】游戏中的贝塞尔曲线以及其在Unity中的实现

    RT,马三最近在参与一款足球游戏的开发,其中涉及到足球的各种运动轨迹和路径,比如射门的轨迹,高吊球,香蕉球的轨迹.最早的版本中马三是使用物理引擎加力的方式实现的足球各种运动,后来的版本中使用了根据物理 ...

  9. iOS - Quartz 2D 贝塞尔曲线

    1.贝塞尔曲线 贝塞尔曲线(Bézier curve),又称贝兹曲线或贝济埃曲线,是应用于二维图形应用程序的数学曲线.一般的矢量图形软件通过它来精确画出曲线,贝兹曲线由线段与节点组成,节点是可拖动的支 ...

随机推荐

  1. 在C#中如何使用资源的方法

    总结一个在C#中如何使用资源的方法:一:使用本地文件1.将本地要加入的资源文本(视频,图片,文本或其它)加入项目,比如我们现在加入一个up.bmp的图片到项目中,且放在文件夹Resources下面,2 ...

  2. SQLServer数据库监控代码

    SQLServer数据库监控代码: creation_time, total_worker_time, last_worker_time, max_worker_time, min_worker_ti ...

  3. Oracle EBS 初始化用户密码(转)

    ---修改密码,并且将限制用户下次登录的时候(第一次登录),强制要换一个新的口令: ---此过程可以完全模拟我们在标准用户的Form里面初始化用户的密码的动作! ---最后要说明的是,这个处理过程是通 ...

  4. (五)SQL入门 数据库查询

    什么是查询?查询就是Select语句对数据库的探究. 查询是一种目的,一种需求,一种期望.是Select语句去实现的.Select语句不是只是指select语句,而是多个子句一起使用得组合. sele ...

  5. JS倒计时功能,给定时间返回规定格式倒计时时间

    重写了一下,增强了通用性,重写时发现月份和年份计算很复杂,因为月份天数不一样,年份又涉及平年闰年; 本人数学实在不佳无法写出算法,哪位大大日后写出来了分享给小弟一份,谢谢~. <!doctype ...

  6. MI卡UID

    卡号是根据第0扇区第0块的UID,高位和低位互换后转10进制后出的数字.一般读卡器都会在左边补0补足10位.

  7. memcache基础知识

    memcached的内存存储机制 Memcached默认情况下采用了名为Slab Allocator的机制分配.管理内存.在该机制出现以前,内存的分配是通过对所有记录简单地进行malloc和free来 ...

  8. 【iPhone手机老提示升级怎么办】

    首先复制链接 https://oldcat.me/web/NOOTA9.mobileconfig  然后到自带浏览器(Safari)打开,点击GO,后会弹出安装,安装即可,如果出现密码的,这个密码是你 ...

  9. hadoop是什么

    Hadoop一直是我想学习的技术,正巧最近项目组要做电子商城,我就开始研究Hadoop,虽然最后鉴定Hadoop不适用我们的项目,但是我会继续研究下去,技多不压身. <Hadoop基础教程> ...

  10. JavaScript:this是什么?

    JavaScript:this是什么?定义:this是包含它的函数作为方法被调用时所属的对象.说明:这句话有点咬嘴,但一个多余的字也没有,定义非常准确,我们可以分3部分来理解它! 1.包含它的函数.2 ...