UIBezierPath类可以创建基于矢量的路径,可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状。

一、UIBezierPath使用:

1、创建path;

2、添加路径到path;

3、将path绘制出来;

 //创建path
path = [UIBezierPath bezierPath];
//添加路径
[path moveToPoint:(CGPoint){,}];
[path addLineToPoint:(CGPoint){,}];
//将path绘制出来
[path stroke];

二、实例

1、绘制多边形

注意:这个类要继承自UIView。

 #import "Draw.h"

 @interface Draw (){

 UIBezierPath *path;

 }

 @end

 - (void)drawRect:(CGRect)rect {

     //线条颜色
UIColor *color = [UIColor orangeColor];
[color set]; //创建path
path = [UIBezierPath bezierPath];
//设置线宽
path.lineWidth = ;
//线条拐角
path.lineCapStyle = kCGLineCapRound;
//终点处理
path.lineJoinStyle = kCGLineJoinRound; [path moveToPoint:(CGPoint){,}];
[path addLineToPoint:(CGPoint){,}];
[path addLineToPoint:(CGPoint){,}];
[path addLineToPoint:(CGPoint){,}];
[path addLineToPoint:(CGPoint){,}];
[path addLineToPoint:(CGPoint){,}];
[path closePath];
//根据坐标点连线 [path stroke]; }

如果修改最后一句代码将[path stroke]改成[path fill];

下面来看看区别,

2、绘制矩形

+ (UIBezierPath *)bezierPathWithRect:(CGRect)rect;

 - (void)drawRect:(CGRect)rect {

     //线条颜色
UIColor *color = [UIColor orangeColor];
[color set]; //创建path
//rect四个值分别为(x、y、矩形长,矩形宽)
path = [UIBezierPath bezierPathWithRect:(CGRect){,,,}];
//设置线宽
path.lineWidth = ;
//线条拐角
path.lineCapStyle = kCGLineCapRound;
//终点处理
path.lineJoinStyle = kCGLineJoinRound; //根据坐标点连线
[path stroke]; }

3、绘制圆形或椭圆形

绘制圆形或椭圆形,我们我用

+ (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect;

这个方法根据传入的rect矩形参数绘制一个内切曲线。
当传入的rect是一个正方形时,绘制的图像是一个内切圆;当传入的rect是一个长方形时,绘制的图像是一个内切椭圆。
 - (void)drawRect:(CGRect)rect {

     //线条颜色
UIColor *color = [UIColor orangeColor];
[color set]; //添加路径
path = [UIBezierPath bezierPathWithOvalInRect:(CGRect){,,,}];
path.lineWidth = ;
//线条拐角
path.lineCapStyle = kCGLineCapRound;
//终点处理
path.lineJoinStyle = kCGLineJoinRound;
//根据坐标点连线
[path stroke]; }

下面改变rect值,

path = [UIBezierPath bezierPathWithOvalInRect:(CGRect){50,50,100,50}];

4、绘制弧线

绘制弧线用方法:

+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center

                   radius:(CGFloat)radius

                 startAngle:(CGFloat)startAngle

                  endAngle:(CGFloat)endAngle

                  clockwise:(BOOL)clockwise;

其中 Center:圆弧的中心;

    radius:半径;

startAngle:开始角度;

endAngle:结束角度;

clockwise:是否顺时针方向;

#import "Draw.h"
//定义PI值
#define PI 3.14159265359 @interface Draw (){ UIBezierPath *path; } - (void)drawRect:(CGRect)rect { //线条颜色
UIColor *color = [UIColor orangeColor];
[color set]; //添加路径
path = [UIBezierPath bezierPathWithArcCenter:(CGPoint){,}
radius:
startAngle:
endAngle:PI*0.5
clockwise:YES
];
path.lineWidth = ;
//线条拐角
path.lineCapStyle = kCGLineCapRound;
//终点处理
path.lineJoinStyle = kCGLineJoinRound; //根据坐标点连线
[path stroke]; }

5、二次贝塞尔曲线和三次贝塞尔曲线的绘制

曲线段在当前点开始,在指定的点结束;曲线的形状有开始点,结束点,一个或者多个控制点的切线定义。

下图显示了两种曲线类型的相似,以及控制点和curve形状的关系。

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

方法:- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;

 - (void)drawRect:(CGRect)rect {

     //线条颜色
UIColor *color = [UIColor orangeColor];
[color set]; //添加路径
path = [UIBezierPath bezierPath]; path.lineWidth = ;
//线条拐角
path.lineCapStyle = kCGLineCapRound;
//终点处理
path.lineJoinStyle = kCGLineJoinRound; [path moveToPoint:(CGPoint){,}];
[path addQuadCurveToPoint:(CGPoint){,} controlPoint:(CGPoint){,}]; //根据坐标点连线
[path stroke]; }

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

方法:- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;

 - (void)drawRect:(CGRect)rect {

     //线条颜色
UIColor *color = [UIColor orangeColor];
[color set]; //添加路径
path = [UIBezierPath bezierPath]; path.lineWidth = ;
//线条拐角
path.lineCapStyle = kCGLineCapRound;
//终点处理
path.lineJoinStyle = kCGLineJoinRound; [path moveToPoint:(CGPoint){,}];
[path addCurveToPoint:(CGPoint){,} controlPoint1:(CGPoint){,} controlPoint2:(CGPoint){,}]; //根据坐标点连线
[path stroke]; }

ios基础篇(二十)—— UIBezierPath绘制的更多相关文章

  1. ios基础篇(十四)——UITableView(二)属性及基本用法

    上一篇说了UITableView的重用机制,让我们对UITableView有了简单了解,下面说说UITableView的属性及常见方法. 一.属性 1.frame:设置控件的尺寸和大小 2.backg ...

  2. ios基础篇(十二)——UINavgationController的使用(三)ToolBar

    UIToolBar存在于UINavigationController导航栏控制器中,而且默认被隐藏:设置UINavigationController的toolbarHidden属性可显示UIToolB ...

  3. ios基础篇(十六)——UIWebView的基本使用

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档等.UIWebView是一个混合体,具体的功能控件内置的,实现一些基本的功能.UIWebView可以查看Html网页,pdf文件,do ...

  4. ioS基础篇(十九)——UIResponder简析

    UIResponder类定义了对象相应和控制事件的接口,他是UIApplication.UIView的超类,这类的实例通常被称为应答对象. 一.Responder对象 在iOS系统中,能够响应并处理事 ...

  5. ios基础篇(十八)——Delegate 、NSNotification 和 KVO用法及其区别

    一.Delegate Delegate本质是一种程序设计模型,iOS中使用Delegate主要用于两个页面之间的数据传递.iphone中常用@protocol和delegate的机制来实现接口的功能. ...

  6. iOS基础篇(十五)——UIScrollView的基本用法

    滚动视图(UIScrollView)通常用于显示内容尺寸大于屏幕尺寸的视图. 一.基本属性 1.CGSize contentSize :设置UIScrollView的滚动范围 2.CGPoint co ...

  7. ios基础篇(十)——UINavgationController的使用(一)UIBarButtonItem的添加

    UINavigationController又被成为导航控制器,继承自UIViewController,以栈的方式管理所控制的视图控制器,下面就详细说一下UINavigationController的 ...

  8. php基础篇-二维数组排序 array_multisort

    原文:php基础篇-二维数组排序 array_multisort 对2维数组或者多维数组排序是常见的问题,在php中我们有个专门的多维数组排序函数,下面简单介绍下: array_multisort(a ...

  9. C#学习基础概念二十五问

    C#学习基础概念二十五问 1.静态变量和非静态变量的区别?2.const 和 static readonly 区别?3.extern 是什么意思?4.abstract 是什么意思?5.internal ...

  10. ios基础篇(二十五)—— Animation动画(UIView、CoreAnimation)

    Animation主要分为两类: 1.UIView属性动画 2.CoreAnimation动画 一.UIView属性动画 UIKit直接将动画集成到UIView类中,实现简单动画的创建过程.UIVie ...

随机推荐

  1. DuiLib事件分析(一)——鼠标事件响应

    最近在处理DuiLib中自定义列表行元素事件,因为处理方案得不到较好的效果,于是只好一层一层的去剥离DuiLib事件是怎么来的,看能否在某一层截取消息,自己重写. 我这里使用CListContaine ...

  2. 基于eBox的LTC1446驱动

    LTC1446 是linear出品的双通道12bit轨对轨DAC芯片,采用SPI接口,内部基准电压,满量程输出4.095v,单电源供电(4.5-5v).8Pin封装.            使用时非常 ...

  3. SQL2005中的事务与锁定(七) - 转载

    ------------------------------------------------------------------------ -- Author : HappyFlyStone - ...

  4. Java 可变参数列表

    1.可变参数列表的简单实现 当调用方法时,方法的参数个数或类型未知时,称其为可变参数列表.在以前的Java代码中,可以使用Object数组来实现这样的功能.因为,所有的类都是直接或间接继承于Objec ...

  5. CentOS 7 防火墙和端口配置

    centos 7 防火墙和端口配置--解决 RHEL 7/ CentOS 7/Fedora 出现Unit iptables.service failed to load # 第一步,关闭firewal ...

  6. androidBroadCast总结

    BoradCast广播1.接受广播 BroadCastReceiver(接收系统的广播) 1-1:电话的广播 1-1-1:拨打电话的广播 1.创建一个类,继承BoradcastReceiver 2.重 ...

  7. 相似性度量(Similarity Measurement)与“距离”(Distance)

    在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间的“距离”(Distance).采用什么样的方法计算距离是很讲究,甚至关 ...

  8. 基于Opencv和Mfc的图像处理增强库GOCVHelper(索引)

    GOCVHelper(GreenOpen Computer Version Helper )是我在这几年编写图像处理程序的过程中积累下来的函数库.主要是对Opencv的适当扩展和在实现Mfc程序时候的 ...

  9. PHP 可变长度参数列表

    In PHP 5.6 and later, argument lists may include the ... token to denote that the function accepts a ...

  10. sqlite与android交互 (封装)

    学android已经有大概一周时间了吧 ,总感觉自己基础不怎么好,只能通过一点一点积累着敲来巩固平常的知识,有的时候就先不封装的敲一遍,再封装上,有些语句真的记不住,虽然知道他是什么意思,于是乎就反复 ...