zoukankan      html  css  js  c++  java
  • Quartz 2D简单的绘图

    1. 利用CG框架绘图的步骤:
    2.  1.子类化UIView
    3.  2.在GraphicView中复写drawRect方法
    4.  3.获取图形上下文对象
    5.  4.绘制线条
    6.  (1)先创建一个路径,把线条定义在这个路径上,再把路径交给context去显示。
    7.  (2)直接把线条绘制到context上去。
    8.  5.绘制简单图形
    9.  6.绘制文本

    10. 7.绘制图像

    12.- (void)drawRect:(CGRect)rect {

    13. //1.获取图形上下文

    1. CGContextRef context = UIGraphicsGetCurrentContext();//图形上下文
    2. [self drawImage:context];

    17.}

    19.////绘制线条方法一

    20.//2.创建一个绘制路径

    1. CGMutablePathRef path = CGPathCreateMutable();

    22.//在路径上添加一个起点

    1. CGPathMoveToPoint(path, NULL, 50, 50);
    2. CGPathAddLineToPoint(path, NULL, 100, 100);

    25.//绘制完毕后关闭路径,这样会把起始点和终点连接

    1. CGPathCloseSubpath(path);

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

    1. CGContextAddPath(context, path);

    29.//4.设置图形上下文的属性

    1. //设置线宽
    2. CGContextSetLineWidth(context, 5);
    3. //设置线条颜色
    4. CGContextSetRGBStrokeColor(context, 254.0/255, 201.0/255, 21.0/255, 1);
    5. //填充颜色
    6. CGContextSetRGBFillColor(context, 0, 1, 0, 1);

    36.//5.在图形上下文上绘制路径

    1. /*
    2. 绘制模式:
    3. kCGPathFill:填充(实心)
    4. kCGPathStroke:画线(空心)
    5. kCGPathFillStroke :即画线又填充
    6. */
    7. CGContextDrawPath(context, kCGPathFillStroke);

    44.//6.释放路径,有create就要有release

    1. CGPathRelease(path);

    47.////绘制线条方法二

    48.CGPoint points[] = {{50,50},{100,100},{50,100},{50,50}};

    1. CGContextAddLines(context, points, 4);

    50.//设置线条颜色

    1. [[UIColor redColor] setStroke];

    52.//设置填充颜色

    1. [[UIColor blueColor] setFill];

    54.//同时设置线条和填充颜色

    1. [[UIColor redColor] set];

    56.CGContextDrawPath(context, kCGPathFillStroke);

    58.//绘制矩形

    59.//1.使用CG提供的函数

    1. CGRect rect = CGRectMake(10, 10, 100, 200);
    2. //添加到图形上下文中
    3. CGContextAddRect(context, rect);
    4. //设置context的属性
    5. [[UIColor redColor] set];   
    6. CGContextDrawPath(context, kCGPathFillStroke);

    66.//2.使用UIKit框架提供的函数

    1. CGRect rect = CGRectMake(10, 10, 100, 200);
    2. //设置context的属性
    3. [[UIColor redColor] set];
    4. //绘制实心矩形
    5. //UIRectFill(rect);
    6. //绘制空心矩形
    7. UIRectFrame(rect);

    76.//绘制一个圆形

    77.//1.

    1. x,y圆心的坐标
    2. radius:半径
    3. startAngel:起始角度
    4. endAngel:结束的角度
    5. clockwise:0顺时针,1逆时针
    6. CGContextAddArc(context, 160, 160, 100, 0, M_PI_4, 0);

    84.//2.给定一个矩形,绘制一个内切圆

    1. CGRect rect = CGRectMake(50, 50, 100, 100);
    2. [[UIColor redColor] set];
    3. UIRectFrame(rect);
    4. CGContextAddEllipseInRect(context, rect);
    5. CGContextDrawPath(context, kCGPathFillStroke);

    91.//绘制贝赛尔曲线

    92.//设置起点

    1. CGContextMoveToPoint(context, 20, 200);
    2. //CGContextAddCurveToPoint(context, 100, 20, 200, 300, 300, 50);
    3. CGContextAddQuadCurveToPoint(context, 150, 20, 300, 200);
    4. [[UIColor redColor] set];
    5. CGContextDrawPath(context, kCGPathStroke);

    98.//绘制一个文本

    1. NSString *str = @"hello world";
    2.     
    3.     CGRect rect = CGRectMake(50, 50, 200, 300);
    4.     
    5.     [[UIColor whiteColor] setFill];
    6.     
    7.     UIRectFill(rect);
    8.     
    9.     UIFont *font = [UIFont systemFontOfSize:20];
    10.     UIColor *color = [UIColor redColor];
    11.     
    12.     //创建段落样式对象,通过对其属性的设置可以设置文字在段落中显示的样式。
    13.     NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    14.     style.lineBreakMode = NSLineBreakByWordWrapping;
    15.     style.alignment = NSTextAlignmentCenter;
    16.     
    17. //    [str drawInRect:rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
    18.     
    19.     [str drawInRect:rect withAttributes:
    20.   @{
    21.     NSFontAttributeName:font,
    22.     NSForegroundColorAttributeName:color,
    23.     NSParagraphStyleAttributeName:style
    24.     }];
    25. //这种绘制文本的方式非常轻量级,不需要创建一个UILabel控件,并且可以根据需求定制效果。
    26.  
    27. //绘制图片
    28. //1.使用UIKit提供的方法绘制
    29.     UIImage *image = [UIImage imageNamed:@"mp3.jpg"];
    30.     图片拉伸在指定矩形区域显示
    31.     [image drawInRect:CGRectMake(0, 0, 320, 200)];
    32.     图片平铺在指定区域显示
    33.     [image drawAsPatternInRect:CGRectMake(0, 0, 320, 200)];
    34. //2.用CoreGraphic提供的函数绘制
    35.     //CG坐标系统转化为UIKit的坐标系统
    36.     CGContextSaveGState(context);
    37.     //(1)顺时针旋转180度
    38.     CGContextRotateCTM(context, M_PI);
    39.     //(2)x缩放为原来的-1倍
    40.     CGContextScaleCTM(context, -1, 1);
    41.     //(3)向上平移
    42.     CGContextTranslateCTM(context, 0, -image.size.height);
    43.     CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
    44.     //(4)恢复context
    45.     CGContextRestoreGState(context);
  • 相关阅读:
    CheckBox循环删除代码
    最小二乘法原理
    break_VS_continue
    check the sentence &ff
    check_return
    check_FunctionAddress
    while执行两次的问题,已经解决
    CalculationWithDifferenceTpye
    SaveAboutZero
    check_negation
  • 原文地址:https://www.cnblogs.com/SilverWinter/p/4423424.html
Copyright © 2011-2022 走看看