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. [Android Tips] 8. Install apk on multiple connected devices

    $ adb devices | | | xargs -I X adb -s X install pathto/myapp-release.apk

  2. linux--------wdcp中的各种坑。

    1.刚买的空间客服给安装了wdcplinux,结果上去一看PHP是5.2版本的,这不是搞笑嘛.然后就有了下面的升级: 复制这条命令回车然后敲Y就可以: wget http://soft.itbulu. ...

  3. 夺命雷公狗-----React---13--事件监听

    在react中事件监听直接作为组建的属性来添加即可,就像DOM中的html操作 <!DOCTYPE> <html> <head> <meta charset= ...

  4. Qt之自定义信号和槽函数

    自定义信号和槽函数: 1.类的声明和实现分别放在.h和.cpp文件中: 2.类声明包含Q_OBJECT宏: 3.信号只要声明不要设计其的实现函数 4.发射信号用emit关键字 5.自定义槽的实现与普通 ...

  5. [已解决]Eclipse 插件Maven在使用 add dependency,找不到包,解决办法

    以Eclipse版本[Version: Luna Release (4.4.0),]为例, 依次打开:Window >show view > other > Maven Reposi ...

  6. SUDTOJ 3323园艺问题 (线段树)

    园艺问题 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 本巨养了一盆双色茉莉.这种花有一种特点:第i朵花在第Di天盛开,刚开时是紫色的 ...

  7. Balsamiq Mockups 注册码

    Blacklist: Organization name: Rick DongSerial Key: eNrzzU/OLi0odswsqgnKTM5WcMnPS1eoMTQyMjexMDQyAIEa5 ...

  8. 14.KVM安装之脚本和镜像目录树准备

    1.php脚本需要先安装PHP环境,Apache服务器必须支持PHP $ yum install -y php    #安装PHP $ php -v                      #查看是 ...

  9. immutableJS一些API

    原生js转换为immutableData Immutable.fromJS([1,2]) // immutable的 list Immutable.fromJS({a: 1}) // immutabl ...

  10. 深入浅出设计模式——模板方法模式(Template Method Pattern)

    模式动机 模板方法模式是基于继承的代码复用基本技术,模板方法模式的结构和用法也是面向对象设计的核心之一.在模板方法模式中,可以将相同的代码放在父类中,而将不同的方法实现放在不同的子类中.在模板方法模式 ...