概述
UIBezierPath用于定义一个由直线/曲线组合而成的路径, 并且可以在自定义视图中渲染该路径. 在使用的过程中, 我们只需要先指定好路径的结构, 比如一条直线、一条贝塞尔曲线、一个矩形、一个椭圆、一个圆弧等, 然后使用系统为我们提供的方法将构建好的路径渲染出来即可。UIBezierPath位于UIKit库中, 是针对Core Graphics库中的CGPathRef的封装
1.基本画线
// 创建path
UIBezierPath *path = [UIBezierPath bezierPath];
// 添加路径1条点(100,100)到点(200,100)的线段]到path
[path moveToPoint:CGPointMake(100 , 100)];
[path addLineToPoint:CGPointMake(200, 100)];
// 将path绘制出来
[path stroke];
2.矩形
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 10, 100, 100)];//矩形
path.lineWidth = 5;//边框宽度
path.lineCapStyle = kCGLineCapRound;//线条拐角样式
path.lineJoinStyle = kCGLineCapRound;//终点拐角样式
[[UIColor redColor] set];//设置填充颜色
[[UIColor orangeColor] setStroke];//边框线条色
[path closePath];//关闭路径
[path stroke];//渲染 绘制
[path fill];//填充颜色 不会遮挡边框色
3.圆
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 10, 100, 100)];//圆
//UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 10, 100, 50)];//椭圆
path.lineWidth = 5;//边框宽度
[[UIColor redColor] set];//设置填充颜色
[[UIColor orangeColor] setStroke];//边框线条色
[path closePath];//关闭路径
[path stroke];//渲染 绘制
[path fill];//填充颜色 不会遮挡边框色
4.圆弧
#define DEGREES_TO_RADIANS(degrees) ((degrees)*M_PI)/180
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:50 startAngle:0 endAngle:DEGREES_TO_RADIANS(120) clockwise:YES];//clockwise YES顺时针 NO逆时针
path.lineWidth = 2;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
[[UIColor redColor] setFill];
[path stroke];
5.贝塞尔曲线
// 第一种
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 2;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
[path moveToPoint:CGPointMake(100, 100)];
[path addQuadCurveToPoint:CGPointMake(250, 100) controlPoint:CGPointMake(150, 10)];
[[UIColor redColor] setStroke];
[path stroke];
//第二种
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 2;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
[path moveToPoint:CGPointMake(100, 100)];
[path addCurveToPoint:CGPointMake(250, 100) controlPoint1:CGPointMake(150, 0) controlPoint2:CGPointMake(180, 120)];
[[UIColor redColor] setStroke];
[path stroke];
实例:
UIBezierPath *path = [UIBezierPath bezierPath];
CGSize size = self.bounds.size;
CGFloat margin = 10;
CGFloat radius = rintf(MIN(size.height - margin, size.width - margin)/4);
CGFloat xOffset, yOffset;
CGFloat offset = rintf((size.height - size.width) / 2);
if (offset > 0) {
xOffset = (CGFloat)rintf(margin/2);
yOffset = offset;
}else{
xOffset = -offset;
yOffset = rintf(margin/2);
}
[[UIColor redColor] setFill];
[path addArcWithCenter:CGPointMake(radius * 2 + xOffset, radius + yOffset)
radius:radius
startAngle:(CGFloat)-M_PI
endAngle:0
clockwise:YES];
[path addArcWithCenter:CGPointMake(radius * 3 + xOffset, radius * 2 + yOffset)
radius:radius
startAngle:(CGFloat)-M_PI_2
endAngle:(CGFloat)M_PI_2
clockwise:YES];
[path addArcWithCenter:CGPointMake(radius * 2 + xOffset, radius * 3 + yOffset)
radius:radius
startAngle:0
endAngle:(CGFloat)M_PI
clockwise:YES];
[path addArcWithCenter:CGPointMake(radius + xOffset, radius * 2 + yOffset)
radius:radius
startAngle:(CGFloat)M_PI_2
endAngle:(CGFloat)-M_PI_2
clockwise:YES];
[path closePath];
[path fill];
6.CAShapeLayer与UIBezierPath
CGPoint point1 = CGPointMake(50, 10);
CGPoint point2 = CGPointMake(200, 10);
CGPoint point3 = CGPointMake(200, 40);
CGPoint point4 = CGPointMake(230, 40);
CGPoint point5 = CGPointMake(200, 60);
CGPoint point6 = CGPointMake(200, 150);
CGPoint point7 = CGPointMake(50, 150);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:point1];
[path addLineToPoint:point2];
[path addLineToPoint:point3];
[path addLineToPoint:point4];
[path addLineToPoint:point5];
[path addLineToPoint:point6];
[path addLineToPoint:point7];
[path closePath];
CAShapeLayer *shapelayer = [CAShapeLayer layer];
shapelayer.path = path.CGPath;
shapelayer.fillColor = [[UIColor orangeColor] CGColor];
[self.layer addSublayer:shapelayer];