iOS UIBezierPath CAShapeLayer

此类可以创建路径,是Core Graphics框架关于path的一个封装

创建path的步骤

  1. 创建一个BezierPath对象
UIBezierPath *path = [UIBezierPath bezierPath];

//根据传入的rect矩形参数绘制一个内切曲线;当传入的rect是一个正方形时,绘制的图像是一个内切圆;当传入的rect是一个长方形时,绘制的图像是一个内切椭圆
+ (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect

//rect 绘制矩形的 frame,corners指定使哪个角为圆角
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;

/**
 创建一段弧线

 @param center 中心
 @param radius 半径
 @param startAngle 开始角度
 @param endAngle 结束角度
 @param getRandomPoint 是否顺时针方向
 @return qqq
 */
+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center
                                   radius:(CGFloat)radius
                               startAngle:(CGFloat)startAngle
                                 endAngle:(CGFloat)endAngle
                                clockwise:(BOOL)clockwise


  1. 使用moveToPoint:方法设置初始线段的起点
[path moveToPoint:CGPointMake(0, 0)];
  1. 添加line或者curve
    [path addLineToPoint:CGPointMake(codeSize*0.5-offset,0)];
    [path addQuadCurveToPoint:CGPointMake(codeSize*0.5+offset, 0) controlPoint:CGPointMake(codeSize*0.5, -offset*2)];//上方的弧线
    [path addLineToPoint:CGPointMake(codeSize, 0)];
    
    [path addLineToPoint:CGPointMake(codeSize,codeSize*0.5-offset)];
    [path addQuadCurveToPoint:CGPointMake(codeSize, codeSize*0.5+offset) controlPoint:CGPointMake(codeSize+offset*2, codeSize*0.5)];//右侧的弧线
    [path addLineToPoint:CGPointMake(codeSize, codeSize)];
    
    [path addLineToPoint:CGPointMake(codeSize*0.5+offset,codeSize)];
    [path addQuadCurveToPoint:CGPointMake(codeSize*0.5-offset, codeSize) controlPoint:CGPointMake(codeSize*0.5, codeSize-offset*2)];//下面的凹线
    [path addLineToPoint:CGPointMake(0, codeSize)];
    
    [path addLineToPoint:CGPointMake(0,codeSize*0.5+offset)];
    [path addQuadCurveToPoint:CGPointMake(0, codeSize*0.5-offset) controlPoint:CGPointMake(0+offset*2, codeSize*0.5)];//左侧的凹线
    [path addLineToPoint:CGPointMake(0, 0)];
    //它也在最后一个点和第一个点之间画一条线段,如果我们画多边形的话,这个一个便利的方法我们不需要去画最后一条线
    //[path closePath];
  1. 连线或填充
[path stroke];
//[path fill];
属性
  • lineCapStyle

路径的终点形状, 该属性适用于开放路径的起点和终点, 默认为kCGLineCapButt(方形结束, 结束位置正好为精确位置), 其他可选项为kCGLineCapRound(圆形结束, 结束位置超过精确位置半个线宽)和kCGLineCapSquare(方形结束, 结束位置超过精确位置半个线宽)

  • lineJoinStyle

路径的连接点形状, 默认为kCGLineJoinMiter(全部连接), 其他可选项为kCGLineJoinRound(圆形连接)和kCGLineJoinBevel(斜角连接)

iOS UIBezierPath CAShapeLayer_第1张图片
弧线的参考系.png
例子:创建圆弧
- (void)drawRect:(CGRect)rect {
    UIColor *color = [UIColor redColor];
    [color set];
    UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:75 startAngle:0 endAngle:DEGREES_TO_RADIANS(135) clockwise:YES];
    aPath.lineWidth = 5.0;
    aPath.lineCapStyle = kCGLineCapRound;//线条拐角
    aPath.lineJoinStyle = kCGLineCapRound;//终点处理
    [aPath stroke];
}
iOS UIBezierPath CAShapeLayer_第2张图片
弧线.png

指定角为圆角

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0,
    UIRectCornerTopRight    = 1 << 1,
    UIRectCornerBottomLeft  = 1 << 2,
    UIRectCornerBottomRight = 1 << 3,
    UIRectCornerAllCorners  = ~0UL
};
- (void)drawRect:(CGRect)rect {
    
    UIColor *color = [UIColor redColor];
    [color set];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
    
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    path.lineWidth = 5.0;
    
    [path stroke];
}
绘制二次贝塞尔曲线
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
iOS UIBezierPath CAShapeLayer_第3张图片
控制点与开始结束点之间的关系.png

CAShapeLayer

CAShapeLayer是在其坐标系统内绘制贝塞尔曲线(UIBezierPath)的,它有一个path属性,而UIBezierPath就是对CGPathRef类型的封装,因此这两者配合起来使用才可以。CAShapeLayer在提供的路径中进行渲染。路径会闭环,所以绘制出了Shape 用于CAShapeLayer的贝塞尔曲线作为path,其path是一个首尾相接的闭环的曲线,即使该贝塞尔曲线不是一个闭环的曲线

[self.moveImage.layer addSublayer:({//边框
        CAShapeLayer *borderLayer = [self getBorderLayer];
        borderLayer;
    })];

- (CAShapeLayer *)getBorderLayer {
    CGRect bounds = CGRectMake(0, offset, codeSize+offset, codeSize+offset);
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = bounds;
    [shapeLayer setFillRule:kCAFillRuleEvenOdd];
    shapeLayer.fillColor = [[UIColor clearColor] CGColor];
    UIBezierPath *path = getCodePath();
    shapeLayer.path = path.CGPath;
    shapeLayer.shadowOffset = CGSizeMake(2, 2);
    shapeLayer.shadowPath = path.CGPath;
    UIColor *testColor = [UIColor colorWithRed:247/256.0 green:63/256.0 blue:94/256.0 alpha:1.0];
    shapeLayer.shadowColor = testColor.CGColor;
    shapeLayer.strokeColor = testColor.CGColor;
    shapeLayer.lineWidth = 1;
    
    return shapeLayer;
}

iOS UIBezierPath类 介绍
UIBezierPath

你可能感兴趣的:(iOS UIBezierPath CAShapeLayer)