1.介绍
UIBezierPath这个类在UIKit中, 是Core Graphics框架关于path的一个封装,使用此类可以定义简单的形状,比如我们常用到,矩形,圆形,椭圆,弧,或者不规则的多边形
每一个直线段或者曲线段的结束的地方是下一个的开始的地方。每一个连接的直线或者曲线段的集合成为subpath。一个UIBezierPath对象定义一个完整的路径包括一个或者多个subpaths。
我们一般使用UIBezierPath都是在重写view的drawRect方法这种情形。我们用直线去创建矩形或多边形,使用曲线创建弧或者圆。
使用步骤:
1、 重写View的drawRect方法
2、 创建UIBezierPath的对象
3、 使用方法moveToPoint: 设置初始点
4、 根据具体要求使用UIBezierPath类方法绘图(比如要画线、矩形、圆、弧?等)
5、 设置UIBezierPath对象相关属性 (比如lineWidth、lineJoinStyle、aPath.lineCapStyle、color)
6、 使用stroke 或者 fill方法结束绘图
在介绍其他使用方法之前,我们先来看一下 path 的几个属性,以便下面我进行设置。
1、 [color set]; 设置线条颜色,也就是相当于画笔颜色
2、 path.lineWidth = 5.0; 这个很好理解了,就是划线的宽度
3、 path.lineCapStyle 这个线段起点是终点的样式,这个样式有三种:
(
1、 kCGLineCapButt 该属性值指定不绘制端点, 线条结尾处直接结束。这是默认值。
2、 kCGLineCapRound 该属性值指定绘制圆形端点, 线条结尾处绘制一个直径为线条宽度的半圆。
3、 kCGLineCapSquare 该属性值指定绘制方形端点。 线条结尾处绘制半个边长为线条宽度的正方形。需要说明的是,这种形状的端点与“butt”形状的端点十分相似,只是采用这种形式的端点的线条略长一点而已
)
4、 path.lineJoinStyle 这个属性是用来设置两条线连结点的样式,同样它也有三种样式供我们选择
(
1、 kCGLineJoinMiter 斜接
2、 kCGLineJoinRound 圆滑衔接
3、 kCGLineJoinBevel 斜角连接
)
5、 [path stroke]; 用 stroke 得到的是不被填充的 view , [path fill]; 用 fill 得到的内部被填充的 view,这点在下面的代码还有绘制得到的图片中有,可以体会一下这两者的不同。
2.一些简单使用代码
(1) 直线
- (void)drawView1 { UIColor *color = [UIColor redColor]; [color set]; //设置线条颜色 UIBezierPath *myPath = [UIBezierPath bezierPath]; [myPath moveToPoint:CGPointMake(200, 50)]; [myPath addLineToPoint:CGPointMake(300, 100)]; myPath.lineWidth = 5.0; myPath.lineCapStyle = kCGLineCapRound; //线条拐角 myPath.lineJoinStyle = kCGLineJoinRound; //终点处理 [myPath stroke]; }
(2) 多边形
- (void)drawView2 { UIColor *color = [UIColor redColor]; [color set]; //设置线条颜色 UIBezierPath *myPath = [UIBezierPath bezierPath]; [myPath moveToPoint:CGPointMake(200, 50)]; [myPath addLineToPoint:CGPointMake(300, 100)]; [myPath addLineToPoint:CGPointMake(260, 200)]; [myPath addLineToPoint:CGPointMake(100, 200)]; [myPath addLineToPoint:CGPointMake(100, 70)]; [myPath closePath]; myPath.lineWidth = 5.0; myPath.lineCapStyle = kCGLineCapRound; //线条拐角 myPath.lineJoinStyle = kCGLineJoinRound; //终点处理 // [myPath fill];//颜色填充 [myPath stroke]; //内部无色 //closePath方法不仅结束一个shape的subpath表述,它也在最后一个点和第一个点之间画一条线段,这个一个便利的方法我们不需要去画最后一条线了 }
(3) 矩形正方形 和一些特殊
- (void)drawView3 { UIColor *color = [UIColor redColor]; [color set]; // UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 100, 100)]; // 带弧度的矩形 // UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(25, 15, 100, 80) cornerRadius:10]; // 部分圆角 UIBezierPath *path =[UIBezierPath bezierPathWithRoundedRect:CGRectMake(25, 45, 90, 90) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)]; path.lineWidth = 5.0; path.lineCapStyle = kCGLineCapRound; //线条拐角 path.lineJoinStyle = kCGLineJoinRound; //终点处理 [path stroke]; }
(4) 椭圆 圆 + (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect
- (void)drawView4 { UIColor *color = [UIColor redColor]; [color set]; UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 100, 80)]; path.lineWidth = 5.0; path.lineCapStyle = kCGLineCapRound; path.lineJoinStyle = kCGLineJoinRound; [path stroke]; }
(5) 弧线 + (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwis
/* ArcCenter: 原点 radius: 半径 startAngle: 开始角度 endAngle: 结束角度 弧度制 X轴正方形为0 圆2*3.14 clockwise: 是否顺时针方向 / */ - (void)drawView5 { UIColor *color = [UIColor redColor]; [color set]; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:90 startAngle:0 endAngle:DEGREES_TO_RADIANS(90) clockwise:YES]; path.lineWidth = 5.0; path.lineCapStyle = kCGLineCapRound; path.lineJoinStyle = kCGLineJoinRound; [path stroke]; }
(6) 二级贝塞尔曲线 - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
// 这个方法绘制二次贝塞尔曲线。曲线段在当前点开始,在指定的点结束, /* endpoint 终点 controlPoint 切点 两条曲线的切线的交点 */ - (void)drawView6 { UIColor *color = [UIColor redColor]; [color set]; UIBezierPath *path = [UIBezierPath bezierPath]; path.lineWidth = 5.0; path.lineCapStyle = kCGLineCapRound; path.lineJoinStyle = kCGLineJoinRound; [path moveToPoint:CGPointMake(40, 150)]; [path addQuadCurveToPoint:CGPointMake(30, 400) controlPoint:CGPointMake(120, 40)]; [path stroke]; }
(7)绘制三次贝塞尔曲线 - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
// 这个方法绘制三次贝塞尔曲线。曲线段在当前点开始,在指定的点结束,两个控制点的切线定义 /* endPoint 终点 controlPoint1 与起点连线的线与弧线相切 controlPoint2 与终点连线的线与弧线相切 */ - (void)drawView7 { UIColor *color = [UIColor redColor]; [color set]; UIBezierPath *path = [UIBezierPath bezierPath]; path.lineWidth = 5.0; path.lineCapStyle = kCGLineCapRound; path.lineJoinStyle = kCGLineJoinRound; [path moveToPoint:CGPointMake(40, 150)]; [path addCurveToPoint:CGPointMake(260, 200) controlPoint1:CGPointMake(140, 0) controlPoint2:CGPointMake(140, 400)];; [path stroke]; }