zoukankan      html  css  js  c++  java
  • Quartz2D基本图形绘制的3种方法详解

    #pragma mark - 绘图的几种方式

    #pragma mark - 绘图第三种方式

    1.#pragma mark - 最原始的绘图方式

    - (void)drawLine

    {

    // 1.获取图形上下文

    // 目前我们所用的上下文都是以UIGraphics

    // CGContextRef Ref:引用 CG:目前使用到的类型和函数 一般都是CG开头 CoreGraphics

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 2.描述路径

    // 创建路径

    CGMutablePathRef path = CGPathCreateMutable();

    // 设置起点

    // path:给哪个路径设置起点

    CGPathMoveToPoint(path, NULL, 50, 50);

    // 添加一根线到某个点

    CGPathAddLineToPoint(path, NULL, 200, 200);

    // 3.把路径添加到上下文

    CGContextAddPath(ctx, path);

    // 4.渲染上下文

    CGContextStrokePath(ctx);

    }

    2.#pragma mark - 绘图第二种方式

    - (void)drawLine1

    {

    // 获取上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 描述路径

    // 设置起点

    CGContextMoveToPoint(ctx, 50, 50);

    CGContextAddLineToPoint(ctx, 200, 200);

    // 渲染上下文

    CGContextStrokePath(ctx);

    }

    3.// 贝瑟尔路径

    - (void)drawLine2

    {

    // UIKit已经封装了一些绘图的功能

    // 创建路径

    UIBezierPath *path = [UIBezierPath bezierPath];

    // 设置起点

    [path moveToPoint:CGPointMake(50, 50)];

    // 添加一根线到某个点

    [path addLineToPoint:CGPointMake(200, 200)];

    // 绘制路径

    [path stroke];

    //    NSLog(@"%@",NSStringFromCGRect(rect));

    }

    绘制曲线的属性设置

    1.//UIKit封装好的框架-贝塞尔曲线属性设置

    - (void)drawUIBezierPathState

    {

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(50, 50)];

    [path addLineToPoint:CGPointMake(200, 200)];

    path.lineWidth = 10;

    [[UIColor redColor] set];

    [path stroke];

    UIBezierPath *path1 = [UIBezierPath bezierPath];

    [path1 moveToPoint:CGPointMake(0, 0)];

    [path1 addLineToPoint:CGPointMake(30, 60)];

    [[UIColor greenColor] set];

    path1.lineWidth = 3;

    [path1 stroke];

    }

    2.原生绘制的图形的具体属性设置

    - (void)drawCtxState

    {

    // 获取上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 描述路径

    //起点

    CGContextMoveToPoint(ctx, 50, 50);

    CGContextAddLineToPoint(ctx, 100, 50);

    // 设置起点

    CGContextMoveToPoint(ctx, 80, 60);

    // 默认下一根线的起点就是上一根线终点

    CGContextAddLineToPoint(ctx, 100, 200);

    // 设置绘图状态,一定要在渲染之前

    // 颜色

    [[UIColor redColor] setStroke];

    // 线宽

    CGContextSetLineWidth(ctx, 5);

    // 设置连接样式

    CGContextSetLineJoin(ctx, kCGLineJoinBevel);

    // 设置顶角样式

    CGContextSetLineCap(ctx, kCGLineCapRound);

    // 渲染上下文

    CGContextStrokePath(ctx);

    }

  • 相关阅读:
    iOS开发中应用程序图标和启动图片的尺寸
    Localization native development region 设置属性(转)
    iOS 开发之 ZBarSDK 二维码扫描自定义二维码扫描页面(二)
    iOS 开发之 ZBarSDK 二维码扫描 静态库不支持64bit完美解决(一)
    iOS 播放Gif动画
    使用GCD异步下载图片 可以中途停止下载
    GCD线程队列 MJ(转)
    iOS 开发之头部滚动展示视图
    iOS 版本更新 (转)
    iOS App打包发布 (转)
  • 原文地址:https://www.cnblogs.com/CJH5209/p/6029765.html
Copyright © 2011-2022 走看看