zoukankan      html  css  js  c++  java
  • UIBezierPath和CAShapeLayer画直线、CGContextRef画直线两种方案

    在view的相关方法中可直接使用UIBezierPath和CAShapeLayer画图形
    - (void)makeBezierPath
    {
        /**
         CAShapeLayer属于QuartzCore框架,继承自CALayer。CAShapeLayer是在坐标系内绘制贝塞尔曲线的,通过绘制贝塞尔曲线,设置shape(形状)的path(路径),从而绘制各种各样的图形以及不规则图形。因此,使用CAShapeLayer需要与UIBezierPath一起使用。
         UIBezierPath对象(贝塞尔曲线),它是CGPathRef数据类型的封装
         CAShapeLayer是一个通过矢量图形而不是bitmap来绘制的图层子类。
        
         相对于Core Graphics绘制图片,使用CAShapeLayer有以下一些优点:
         1、渲染快速。CAShapeLayer使用了硬件加速(使用CPU渲染),绘制同一图形会比用Core Graphics快很多
         2、高效使用内存。一个CAShapeLayer不需要像普通CALayer一样创建一个寄宿图形,所以无论有多大,都不会占用太多的内存
         3、不会被图层边界剪裁掉。一个CAShapeLayer可以在边界之外绘制。
         */
        // 线的路径
        UIBezierPath *linePath = [UIBezierPath bezierPath];
        // 起点
        [linePath moveToPoint:CGPointMake(100, 100)];
        // 其他点
        [linePath addLineToPoint:CGPointMake(160, 160)];
        [linePath addLineToPoint:CGPointMake(180, 120)];
        
        CAShapeLayer *lineLayer = [CAShapeLayer layer];
        
        lineLayer.lineWidth = 2;
        lineLayer.strokeColor = [UIColor greenColor].CGColor;
        lineLayer.path = linePath.CGPath;
        lineLayer.fillColor = nil; // 默认为blackColor
        
        [self.view.layer addSublayer:lineLayer];
        /**
         //绘制矩形
         UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
         //绘制圆形路径
         UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];
         //绘制自带圆角的路径
         UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) cornerRadius:30];
         //指定矩形某一个角加圆角(代码示例为左上角)
         UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 50)];
         */
    }
    CGContextRef画图只能在view的drawRect方法中,drawRect方法系统会默认创建一个上下文
    -(void)drawRect:(CGRect)rect
    {
        [self makeLine];
        [self makeLineTwo];
    }
    - (void)makeLine
    {
        //注意,在drawRect方法中系统会默认创建一个上下文(C语言类型)
        //下面这个方法中的rect参数会传入当前view的frame
        //Graphics Context是图形上下文,可以将其理解为一块画布
        CGContextRef  context = UIGraphicsGetCurrentContext();
        //创建路径
        CGMutablePathRef path =  CGPathCreateMutable();
        //设置起点
        CGPathMoveToPoint(path, NULL, 50, 150);
        //设置终点
        CGPathAddLineToPoint(path, NULL, 100, 150);
        //颜色
        [[UIColor redColor] setStroke];
        //线宽
        CGContextSetLineWidth(context, 5.0);
        //设置连接样式
        CGContextSetLineJoin(context, kCGLineJoinBevel);
        //设置顶角样式
        CGContextSetLineCap(context, kCGLineCapRound);
        //3、把路径添加到上下文
        CGContextAddPath(context, path);
        //4、渲染上下文到View的layer
        CGContextStrokePath(context);
    }
    - (void)makeLineTwo
    {
        //1、获取图形上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        //2、描述路径(底层封装路径)
        CGContextMoveToPoint(ctx, 200, 200);
        CGContextAddLineToPoint(ctx , 250, 250);
        [[UIColor orangeColor] setStroke];
        //3、渲染上下文到View的layer
        CGContextStrokePath(ctx);
    }

    https://www.jianshu.com/p/139f4fbe7b6b

    https://www.jianshu.com/p/a9d39a4946a5

    https://www.cnblogs.com/jaesun/p/iOS-CAShapeLayerUIBezierPath-hua-xian.html

  • 相关阅读:
    解说asp.net core MVC 过滤器的执行顺序
    asp.net core 2.0 Microsoft.Extensions.Logging 文本文件日志扩展
    【技术累积】【点】【java】【30】代理模式
    【技术累积】【点】【java】【29】MapUtils
    【技术累积】【点】【java】【28】Map遍历
    【技术累积】【点】【java】【27】@JSONField
    【技术累积】【点】【java】【26】@Value默认值
    【技术累积】【点】【java】【25】Orderd
    【技术累积】【点】【java】【23】super以及重写重载
    【技术累积】【线】【java】【2】AOP
  • 原文地址:https://www.cnblogs.com/lulushen/p/11163965.html
Copyright © 2011-2022 走看看