绘图1

作者: WeekDiffculty | 来源:发表于2016-09-16 12:57 被阅读0次

iOS中绘图的概念

  1. iOS

  2. iOS
    openGL Quartz UIView DrawRect

1个像素 = 1/72英寸

基本图形的绘制

  • 矩形
- (void) drawRect:(CGRect)rect{
     //绘制矩形
    //1获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2构建区域
    CGContextAddRect(ctx, CGRectMake(10, 10, 100, 100));
    //3颜色边框
    CGContextSetRGBStrokeColor(ctx, 0, 1, 0.5, 1.0);
    [[UIColor orangeColor]setStroke];
    //4描边路径
    CGContextStrokePath(ctx);
    //4边框宽度
    //5填充路径
    CGContextSetLineWidth(ctx, 10);
    //6绘制
    CGContextDrawPath(ctx, kCGPathStroke);
}
矩形
  • 三角形
- (void) drawTriangle{
   //1.绘制图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 线宽
    CGContextSetLineWidth(ctx, 10);
    //2.构建三角形的位置
    CGContextMoveToPoint(ctx, 100, 10);
    //确定第一条线的结束为止
    CGContextAddLineToPoint(ctx, 150, 70);
    //第二条线
    CGContextAddLineToPoint(ctx, 50, 70);
    //第三条线
    CGContextAddLineToPoint(ctx, 100, 10);
    //3.关闭路径
    CGContextClosePath(ctx);
    [[UIColor blackColor]setStroke];
    CGContextStrokePath(ctx);
    //4. 渲染
    //填充颜色
    [[UIColor orangeColor]setFill];
    CGContextFillPath(ctx);
}
  • 绘制虚线
- (void) drawRect:(CGRect)rect{
    //获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //确定第一个点
    CGContextMoveToPoint(ctx, 10, 10);
    CGContextAddLineToPoint(ctx, 100, 100);
    CGContextAddLineToPoint(ctx,10, 80);
    // 设置宽度
    CGContextSetLineWidth(ctx, 10);
    //设置线终点的形状
    CGContextSetLineCap(ctx, kCGLineCapSquare);
    //连线的样式
    //kCGLineJoinMiter 尖的  kCGLineJoinRound原角 kCGLineJoinBevel 平角
    CGContextSetLineJoin(ctx, kCGLineJoinBevel);
    CGContextSetStrokeColorWithColor(ctx, [UIColor orangeColor].CGColor);
    CGContextStrokePath(ctx);
   
#pragma >>>>>>>>>>>>>>>>>>>>>>>>>>虚线
    CGContextSaveGState(ctx);
    //保存当前绘画的状态 与cgcontextrestoreGstate 即 绘制虚线必须先保存当前的绘画状态
    //更新画板,更新上下文
    CGContextRestoreGState(ctx);//绘制虚线
    CGContextSetLineWidth(ctx, 5);
    CGContextMoveToPoint(ctx, 30, 100);//开始点
    CGFloat lenths[] = {10,20,10,40,10};//控制虚线每个小段长度的数组
    //绘制虚线调用的方法
     CGContextSetLineDash(ctx, 0, lenths, 3);
    CGContextAddLineToPoint(ctx, 180, 180);//第一条
    CGContextAddLineToPoint(ctx, 50, 300);//第二条
    //设置绘制模式
    CGContextDrawPath(ctx, kCGPathStroke);
}

虚线
  • 圆或圆弧
- (void) drawRect:(CGRect)rect{
    //1
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2 构建路径
    /**
     * 
     x y 圆心
     cadius 半径
     startAngle 开始角度
     stopAngle 结束角度
     clockwise 时针方向 1逆 0顺
     
     */
    //CGContextAddArc(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#int clockwise#>)
    CGContextAddArc(ctx, 100, 100, 50, M_PI_4, M_PI_2, 1);
    //填充
    [[UIColor redColor] setFill];
    //填充路径
    CGContextFillPath(ctx);
}
画圆
  • 绘制字体
- (void) drawRect:(CGRect)rect{
    NSString *str = @"WeekDiffculty";
    //设置字体颜色  删除线
    NSDictionary *dict =  @{NSForegroundColorAttributeName:[UIColor redColor],NSStrikethroughStyleAttributeName:@1};
    //指定区域绘制文本
    [str drawInRect:CGRectMake(0, 19, 200, 300) withAttributes:dict];
    [self drawRectImage:rect];
    
}
  • 指定区域绘制图片

      UIImage *iamge = [UIImage imageNamed:@"an"];
      [iamge drawInRect:CGRectMake(0, 50, 50, 50)];
    

}


![绘制文字及图片](https://img.haomeiwen.com/i2382884/e3e13e8887c893b7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

  • R|绘图-学习笔记(二)

    tags: 绘图 R的三大绘图系统 1. 基本绘图系统 (base plotting system) 绘图始于空白...

  • 绘图1

    iOS中绘图的概念 iOS iOSopenGL Quartz UIView DrawRect 1个像素 =...

  • Android绘图之PathMeasure(16)

    Android 绘图学习 android绘图之Paint(1)android绘图之Canvas基础(2)Andro...

  • 2020-11-17用ggplot绘制脑图谱

    (1)安装包 (2)基本绘图 (3)高级绘图

  • AutoCAD绘图准备

    AutoCAD绘图准备 编辑:卞茂林 一、 绘图准备 1、 AutoCAD软件 (1) 软件概况 AutoCAD(...

  • iOS 绘图步骤以及常用绘图方式

    1.纯C语言绘图方式 2.通过 创建路径的形式 绘图 (1)相对第一种纯C绘图方式比较 ,在纯C 的绘图基础 多了...

  • R语言笔记Day1(五 作图)

    1. 基础包-绘图函数 1)高级绘图函数 plot() hist() 频率直方图 boxplot() 箱式图 ...

  • 绘图日记(1)

    今天在公众号上,看到这么一张图,觉得挺有意思的,图中有数据,所以想自己画一下。 分析: 图中为每个城市的活动半径是...

  • ggpubr绘图(1)

    使用ggpubr绘制反映指标动态变化的箱图 参考资料:公众号:珠江肿瘤 R语言|ggpubr 包绘图系列(2)[h...

  • 初识canvas(一)

    api 1.getContext() 要在这块画布(canvas)上绘图,需要取得绘图上下文。而取得绘图上下文对象...

网友评论

      本文标题:绘图1

      本文链接:https://www.haomeiwen.com/subject/jglpettx.html