iOS贝塞尔曲线(UIBezierPath)的基本使用方法
2017-09-19 10:50
393 查看
简介
UIBezierPath是对Core Graphics框架的一个封装,使用UIBezierPath类我们可以画出圆形(弧线)或者多边形(比如:矩形)等形状,所以在画复杂图形的时候会经常用到。分析
首先我们先看一下,UIBezierPath有哪些重要的属性:1、 [color set]设置颜色,color为创建的UIColor对象
2、 [path stroke]填充view的线条的颜色,与[color set]配合使用 ,
3、 [path fill]填充整个view内部的颜色,与[color set]配合使用。
4、 path.lineWidth = 5.0; 这个很好理解了,就是划线的宽度
5、 path.lineCapStyle 这个线段起点是终点的样式,这个样式有三种:
kCGLineCapButt
kCGLineCapRound
kCGLineCapSquare
6、 path.lineJoinStyle 这个属性是用来设置两条线连结点的样式,同样它也有三种样式供我们选择
kCGLineJoinMiter 直接连接
kCGLineJoinRound 圆滑衔接
kCGLineJoinBevel 斜角连接
使用
接下来,我们就看一下UIBezierPath到底应该怎么使用:首先,我们先自定义一个UIView的子类,然后重写- (void)drawRect:(CGRect)rect 方法,将创建图形的方法写到该方法中,下面是一些简单的示例:
画多边形
UIBezierPath* aPath = [UIBezierPath bezierPath]; aPath.lineWidth = 15.0; /* kCGLineCapButt, kCGLineCapRound, kCGLineCapSquare */ aPath.lineCapStyle = kCGLineCapButt ; //终点(起点)样式 /* kCGLineJoinMiter, kCGLineJoinRound, kCGLineJoinBevel */ aPath.lineJoinStyle = kCGLineJoinBevel; //拐点样式 [aPath moveToPoint:CGPointMake(150, 30)];//设置起始点 [aPath addLineToPoint:CGPointMake(250, 70)];//途经点 [aPath addLineToPoint:CGPointMake(210, 170)];//途经点 [aPath addLineToPoint:CGPointMake(90, 170)];//途经点 [aPath addLineToPoint:CGPointMake(50, 70)];//途经点 [aPath closePath];//通过调用closePath方法得到最后一条线 UIColor *strokeColor = [UIColor redColor]; [strokeColor set]; [aPath stroke];//设置线条颜色 UIColor *fillColor = [UIColor blueColor]; [fillColor set]; [aPath fill];//填充
![](http://upload-images.jianshu.io/upload_images/1914621-70c6f7bd1f991e92.png?imageMogr2/auto-orient/strip%7CimageView2/2)
多边形.png
如果是创建四边形可直接使用:
UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];
画圆
UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)]; aPath.lineWidth = 5.0; aPath.lineCapStyle = kCGLineCapRound; //线条拐角 aPath.lineJoinStyle = kCGLineCapRound; //终点处理 UIColor *color = [UIColor redColor]; [color set]; [aPath stroke];
![](http://upload-images.jianshu.io/upload_images/1914621-29ef7f749287129f.png?imageMogr2/auto-orient/strip%7CimageView2/2)
弧形.png
如果要画正圆,将rect的width和height设置为相等的值即可。
画弧形
/* ArcCenter: 原点 radius: 半径 startAngle: 开始角度 endAngle: 结束角度 clockwise: 是否是顺时针方向 */ UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 300) radius:80 startAngle:0 endAngle:pi clockwise:NO]; aPath.lineWidth = 5.0; aPath.lineCapStyle = kCGLineCapRound; //线条拐角 aPath.lineJoinStyle = kCGLineCapRound; //终点处理 UIColor *color = [UIColor redColor]; [color set]; //设置线条颜色 [aPath stroke];
![](http://upload-images.jianshu.io/upload_images/1914621-e38bf34a88dab60e.png?imageMogr2/auto-orient/strip%7CimageView2/2)
圆形.png
画二次曲线
UIBezierPath* aPath = [UIBezierPath bezierPath]; aPath.lineWidth = 5.0; aPath.lineCapStyle = kCGLineCapRound; //线条拐角 aPath.lineJoinStyle = kCGLineCapRound; //终点处理 [aPath moveToPoint:CGPointMake(100, 100)];//设置初始点 //终点 controlPoint:切点(并不是拐弯处的高度,不懂的同学可以去看三角函数) [aPath addQuadCurveToPoint:CGPointMake(200, 100) controlPoint:CGPointMake(150, 50)]; UIColor *color = [UIColor redColor]; [color set]; [aPath stroke];
![](http://upload-images.jianshu.io/upload_images/1914621-daad62e89679453a.png?imageMogr2/auto-orient/strip%7CimageView2/2)
二次曲线.png
画三次曲线
UIBezierPath *path2 = [UIBezierPath bezierPath]; path2.lineWidth = 5.0; path2.lineCapStyle = kCGLineCapRound; path2.lineJoinStyle = kCGLineJoinRound; [path2 moveToPoint:CGPointMake(0, 100)]; [path2 addCurveToPoint:CGPointMake(100, 100) controlPoint1:CGPointMake(25, 50) controlPoint2:CGPointMake(75, 150)];//两个切点 UIColor *color = [UIColor redColor]; [color set]; [path2 stroke];
![](http://upload-images.jianshu.io/upload_images/1914621-2d65044318677f3b.png?imageMogr2/auto-orient/strip%7CimageView2/2)
三次曲线.png
以上便是iOS中UIBezierPath最基本的使用方法了,在平时的开发中,我们经常将UIBezierPath与CALayer配合使用,下面是一个简单的例子:
//创建CAShapeLayer对象 CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.frame = CGRectMake(100, 100, 200, 200);//设置shapeLayer的尺寸和位置 shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色为ClearColor //设置线条的宽度和颜色 shapeLayer.lineWidth = 1.0f; shapeLayer.strokeColor = [UIColor redColor].CGColor; //创建一个圆形贝塞尔曲线 UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)]; //将贝塞尔曲线设置为CAShapeLayer的path shapeLayer.path = aPath.CGPath; //将shapeLayer添加到视图的layer上 [self.view.layer addSublayer:shapeLayer];
![](http://upload-images.jianshu.io/upload_images/1914621-486597267a9414f7.png?imageMogr2/auto-orient/strip%7CimageView2/2)
shapeLayer.png
总结:
到此为止,关于UIBezierPath最基本的使用就介绍完了,但是关于UIBezierPath在iOS中还有很多更加神奇的应用,有兴趣的同学可以研究一下。作者:沐泽sunshine
链接:http://www.jianshu.com/p/d7671ec6fbb7
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章推荐
- IOS学习笔记(十)之UIImageView图片视图的基本概念和使用方法
- IOS学习笔记(七)之UISegmentedControl分段控件的基本概念和使用方法
- iOS中正则表达式的基本使用方法
- iOS—CABasicAnimation的基本使用方法
- IOS 基本控件使用方法
- IOS学习笔记(十)之UIImageView图片视图的基本概念和使用方法
- iOS 非ARC基本内存管理系列 5-autorelease方法使用总结
- 【iOS开发-画线】使用贝塞尔曲线(UIBezierPath)画线
- iOS FMDatabase 本地数据库的创建和几个基本使用方法
- iOS FMDatabase 本地数据库的创建和几个基本使用方法 【原创】
- iOS开发——网络篇——NSURLSession,下载、上传代理方法,利用NSURLSession断点下载,AFN基本使用,网络检测,NSURLConnection补充
- ios--UITableview的基本使用方法
- iOS开发UITableView基本使用方法总结1
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
- iOS开发OC基础:OC集合NSSet、NSMutableSet以及NSCountedSet的基本方法的使用
- UIScrollView的基本使用方法和注意事项 - iOS - UI基础知识总结10
- IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法
- iOS开发UITableView基本使用方法总结
- IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法
- iOS开发UITableView基本使用方法总结