使用UIBezierPath绘制图形
当需要画图时我们一般创建一个UIView子类, 重写其中的drawRect方法
再drawRect方法中利用UIBezierPath添加画图
UIBezierPath的使用方法:
#import "BezierView.h"
@implementation BezierView
- (void)drawRect:(CGRect)rect {
// Drawing code
//设置线条颜色
UIColor *color = [UIColor redColor];
[color set];
//创建UIBezierPath
UIBezierPath *apath = ({
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 5.0f; //设置线条宽度
path.lineCapStyle = kCGLineCapRound; //设置拐角
path.lineJoinStyle = kCGLineCapRound; //终点处理
//设置起始点
[path moveToPoint:CGPointMake(, )];
//增加线条
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
//关闭路径
[path closePath];
path;
});
//根据坐标连线
[apath stroke];
}
然后把自定义的View添加到Controller中
#import "ViewController.h"
#import "BezierView.h" #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; BezierView *beView = [[BezierView alloc] initWithFrame:\
CGRectMake(, , SCREEN_WIDTH, SCREEN_HEIGHT)]; beView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:beView]; } @end
运行结果能看到一个多边形

如果把drawRect中最后一句话改为[apath fill];运行结果就是实心图

我们可以用UIBezierPath的bezierPathWithRect:CGRect(rect)方法来画矩形, 代码如下
- (void)drawRect:(CGRect)rect {
// Drawing code
//设置线条颜色
UIColor *color = [UIColor redColor];
[color set];
//创建UIBezierPath
UIBezierPath *apath = [UIBezierPath bezierPathWithRect:CGRectMake(, , , )];
//更具坐标连线
[apath fill];
}
运行结果:

我们可以用UIBezierPath的bezierPathWithOvallInRect:CGRect(rect)方法来画圆形和椭圆, 代码如下
- (void)drawRect:(CGRect)rect {
// Drawing code
//设置线条颜色
UIColor *color = [UIColor redColor];
[color set];
//创建UIBezierPath
UIBezierPath *apath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )];
apath.lineWidth = 5.0f;
apath.lineCapStyle = kCGLineCapRound;
apath.lineJoinStyle = kCGLineCapRound;
//更具坐标连线
[apath stroke];
}
运行结果:

用下面这个方法画带指定远角的矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
上代码:
- (void)drawRect:(CGRect)rect {
// Drawing code
//设置线条颜色
UIColor *color = [UIColor redColor];
[color set];
//创建UIBezierPath
UIBezierPath *apath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(, , , )
byRoundingCorners:UIRectCornerTopLeft
cornerRadii:CGSizeMake(, )];
apath.lineWidth = 5.0f;
apath.lineCapStyle = kCGLineCapRound;
apath.lineJoinStyle = kCGLineCapRound;
//更具坐标连线
[apath stroke];
}
运行结果:

如果要设置多个圆角的话就给byRoundingCorners多设置几个角度, 角度可选如下
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = << ,
UIRectCornerTopRight = << ,
UIRectCornerBottomLeft = << ,
UIRectCornerBottomRight = << ,
UIRectCornerAllCorners = ~0UL
};
例如:
UIBezierPath *apath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(, , , )
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(, )];
就有两个圆角

也可以用下面这个方法画圆弧
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center //圆心坐标
radius:(CGFloat)radius //半径
startAngle:(CGFloat)startAngle //弧形开始的角度
endAngle:(CGFloat)endAngle //弧形结束的角度
clockwise:(BOOL)clockwise; //正向还是反向画弧
上代码:
- (void)drawRect:(CGRect)rect {
// Drawing code
//设置线条颜色
UIColor *color = [UIColor redColor];
[color set];
//创建UIBezierPath
UIBezierPath *apath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(, )
radius: startAngle:M_PI /
endAngle:M_PI
clockwise:YES];
apath.lineWidth = 5.0f;
apath.lineCapStyle = kCGLineCapRound;
apath.lineJoinStyle = kCGLineCapRound;
//更具坐标连线
[apath stroke];
}
运行结果如下

还可以直接在path中添加圆形段
[path addArcWithCenter:CGPointMake(, )
radius: startAngle:M_PI /
endAngle:M_PI clockwise:YES];
最后附上UIBezierPath画圆弧时段坐标系

另外UIBezierPath可以画贝赛尔曲线
下面是添加二次贝赛尔曲线的方法
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
上代码:
- (void)drawRect:(CGRect)rect {
// Drawing code
//设置线条颜色
UIColor *color = [UIColor redColor];
[color set];
//创建UIBezierPath
UIBezierPath *apath = ({
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 2.0f; //设置线条宽度
//path.lineCapStyle = kCGLineCapRound; //设置拐角
//绘制二次贝赛尔曲线
//设置起始点
[path moveToPoint:CGPointMake(, )];
//设置EndPoint & Control Point
[path addQuadCurveToPoint:CGPointMake(, ) controlPoint:CGPointMake(, )];
path;
});
//更具坐标连线
[apath stroke];
}
运行结果为:

可以参照下面这张图看看每个点的定义

三次贝赛尔曲线会有2个控制点

上代码:
- (void)drawRect:(CGRect)rect {
// Drawing code
//设置线条颜色
UIColor *color = [UIColor redColor];
[color set];
//创建UIBezierPath
UIBezierPath *apath = ({
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 2.0f; //设置线条宽度
//绘制三次贝赛尔曲线
//设置起始点
[path moveToPoint:CGPointMake(, )];
//设置EndPoint & Control Point
[path addCurveToPoint:CGPointMake(, )
controlPoint1:CGPointMake(, )
controlPoint2:CGPointMake(, )];
path;
});
//更具坐标连线
[apath stroke];
}
运行结果:

使用UIBezierPath绘制图形的更多相关文章
- 11-UIKit(Storyboard、View的基本概念、绘制图形、UIBezierPath)
目录: 1. Storyboard 2. Views 3. View的基本概念介绍 4. 绘制图形 5. UIBezierPath 回到顶部 1. Storyboard 1.1 静态表视图 1)Sec ...
- CSS 魔法系列:纯 CSS 绘制图形(心形、六边形等)
<CSS 魔法系列>继续给大家带来 CSS 在网页中以及图形绘制中的使用.这篇文章给大家带来的是纯 CSS 绘制五角星.六角形.五边形.六边形.心形等等. 我们的网页因为 CSS 而呈现千 ...
- html5 Canvas绘制图形入门详解
html5,这个应该就不需要多作介绍了,只要是开发人员应该都不会陌生.html5是「新兴」的网页技术标准,目前,除IE8及其以下版本的IE浏览器之外,几乎所有主流浏览器(FireFox.Chrome. ...
- html5 canvas 笔记一(基本用法与绘制图形)
<canvas> 元素 <canvas id="tutorial" width="150" height="150"> ...
- WPF2D绘制图形方法
我们先看看效果如何: xaml文件: <Window x:Class="WPF2D绘制图形方法.MainWindow" xmlns="http://schemas. ...
- HTML5—canvas绘制图形(1)
1.canvas基础知识 canvas元素是HTML5中新增的一个重要的元素,专门用来绘制图形,不过canvas本身不具备画图的能力,在页面中放置了canvas元素,就相当于在页面中放置了一块矩形的“ ...
- 【canvas学习笔记二】绘制图形
上一篇我们已经讲述了canvas的基本用法,学会了构建canvas环境.现在我们就来学习绘制一些基本图形. 坐标 canvas的坐标原点在左上角,从左到右X轴坐标增加,从上到下Y轴坐标增加.坐标的一个 ...
- HTML5使用Canvas来绘制图形
一.Canvas标签: 1.HTML5<canvas>元素用于图形的绘制,通过脚本(通常是javascript)来完成. 2.<canvas>标签只是图形容器,必须使用脚本来绘 ...
- canvas 绘制图形
canvas 绘制图形: 注意: canvas 的宽高设置在行内,否则会使画布(canvas)产生扭曲,绘图变形: <!DOCTYPE html> <html lang=" ...
随机推荐
- 理解加密算法(三)——创建CA机构,签发证书并开始TLS通信
接理解加密算法(一)--加密算法分类.理解加密算法(二)--TLS/SSL 1 不安全的TCP通信 普通的TCP通信数据是明文传输的,所以存在数据泄露和被篡改的风险,我们可以写一段测试代码试验一下. ...
- 缓存工厂之Redis缓存
这几天没有按照计划分享技术博文,主要是去医院了,这里一想到在医院经历的种种,我真的有话要说:医院里的医务人员曾经被吹捧为美丽+和蔼+可亲的天使,在经受5天左右相互接触后不得不让感慨:遇见的有些人员在挂 ...
- nginx源码分析之网络初始化
nginx作为一个高性能的HTTP服务器,网络的处理是其核心,了解网络的初始化有助于加深对nginx网络处理的了解,本文主要通过nginx的源代码来分析其网络初始化. 从配置文件中读取初始化信息 与网 ...
- JS图片上传预览插件制作(兼容到IE6)
其实,图片预览功能非常地常见.很意外,之前遇到上传图片的时候都不需要预览,也一直没有去实现过.现在手上的项目又需要有图片预览功能,所以就动手做了一个小插件.在此分享一下思路. 一.实现图片预览的一些方 ...
- 最好的.NET开源免费ZIP库DotNetZip(.NET组件介绍之三)
在项目开发中,除了对数据的展示更多的就是对文件的相关操作,例如文件的创建和删除,以及文件的压缩和解压.文件压缩的好处有很多,主要就是在文件传输的方面,文件压缩的好处就不需要赘述,因为无论是开发者,还是 ...
- JavaScript基础知识总结(三)
JavaScript语法 七.循环语句 1.while 语法: while (exp) { //statements; } 说明:while (变量<=结束值) { 需执行的代码 } 例子: / ...
- javaScript中的小细节-script标签中的预解析
首先介绍预解析,虽然预解析字面意思很好理解,但是却是出坑出的最多的地方,也是bug经常会有的地方,利用好预解析的特性可以解决很多问题,并且提高代码的质量及数量,浏览器在解析代码前会把变量的声明和函数( ...
- BPM费控管理解决方案分享
一.方案概述费用是除经营成本外企业的最主要支出,费用管理是财务管理的核心之一,加强企业内控管理如:费用申请.费用报销.费用分摊.费用审批.费用控制和费用支付等,通过科学有效的管理方法规范企业费用管理, ...
- sublime text 3 + python配置,完整搭建及常用插件安装
四年的时间,一直使用EmEditor编辑器进行Python开发,之前是做面向过程,只需要将一个单独的py文件维护好即可,用着也挺顺手,但是最近在做面向对象的开发,不同的py文件中相互关联较多,感觉单纯 ...
- [PHP源码阅读]strpos、strstr和stripos、stristr函数
我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...