• Quartz 2D 绘图


    第一步:创建一个视图继承于UIView,然后在drawRect方法里绘图

    //方形1.

    - (void)drawRect:(CGRect)rect {

        

        CGContextRef  context = UIGraphicsGetCurrentContext();

        

        

        

        

        

        //[self Movebes];

           

    }

    - (void)drawRectLayer:(CGContextRef ) contextRef{

        

        CGPoint p1 = CGPointMake(50, 50);

        

        CGPoint p2 = CGPointMake(150, 50);

        

        CGPoint p3 = CGPointMake(150, 150);

        

        CGPoint p4 = CGPointMake(50, 150);

        

        

        

        CGPoint pointArr[] = {p1,p2,p3,p4,p1};

        

        CGContextAddLines(contextRef,pointArr,5);

        

        [[UIColor redColor] setFill];

        [[UIColor yellowColor ] setStroke];

        

        

        

        CGContextDrawPath(contextRef, kCGPathFillStroke);

        

      

    }

    //方形2

    - (void)drawRe:(CGContextRef ) contextRef{

        

        CGRect rect = CGRectMake(30, 30, 250, 250);

        

    //    CGContextAddRect(contextRef, rect);

    //    

    //    CGContextSetLineWidth(contextRef, 30);

    //    

    //    [[UIColor yellowColor]setStroke];

    //    

    //    [[UIColor blueColor]setFill];

    //    CGContextDrawPath(contextRef, kCGPathFill);

       

        //注意顺序

        [[UIColor redColor]setFill];

        

        UIRectFill(rect);

        

        [[UIColor yellowColor]setStroke];

        

         UIRectFrame(rect);

       

    }

    //画圆

    - (void)drawArc:(CGContextRef ) contextRef{

        /*添加弧形对象

         x:中心点x坐标

         y:中心点y坐标

         radius:半径

         startAngle:起始弧度

         endAngle:终止弧度

         closewise:是否逆时针绘制,0则顺时针绘制

         */

        

        

        CGContextAddArc(contextRef, 150, 150, 80, 0, 2*M_PI, 1);

        

        [[UIColor redColor]set];//默认都是红色

        

        CGContextDrawPath(contextRef, kCGPathStroke);

        

       

    }

    //画椭圆

    - (void)drawEllipse:(CGContextRef ) contextRef{

         CGRect rect = CGRectMake(30, 30, 250, 100);

        

            CGContextAddEllipseInRect (contextRef, rect);

        

        

            [[UIColor yellowColor]set];

        

        

            CGContextDrawPath(contextRef, kCGPathFillStroke);

        

        

    }

    //贝塞尔曲线

    - (void)drawCurve:(CGContextRef ) contextRef{

    //绘制曲线

        

        CGContextMoveToPoint(contextRef, 20, 100);

        /*绘制二次贝塞尔曲线

         c:图形上下文

         cpx:控制点x坐标

         cpy:控制点y坐标

         x:结束点x坐标

         y:结束点y坐标

         */

        CGContextAddQuadCurveToPoint(contextRef, 160, 10, 300, 100);

        

        

        CGContextMoveToPoint(contextRef, 20, 500);

        /*绘制三次贝塞尔曲线

         c:图形上下文

         cp1x:第一个控制点x坐标

         cp1y:第一个控制点y坐标

         cp2x:第二个控制点x坐标

         cp2y:第二个控制点y坐标

         x:结束点x坐标

         y:结束点y坐标

         */

        CGContextAddCurveToPoint(contextRef, 80, 300, 240, 500, 300, 300);

        

        [[UIColor redColor]setStroke];

        

        [[UIColor yellowColor]setFill];

        

        CGContextDrawPath(contextRef, kCGPathFillStroke);

    }

    //文字

    - (void)drawtitle:(CGContextRef)contextRef{

        NSString *str = @"约吗?小伙子!大妈,我们不约!!!";

        CGRect rect =  CGRectMake(40, 40, 100, 100);

        

        [[UIColor yellowColor]set];

        

        UIRectFrame(rect);

        

        NSDictionary *dic = @{

                              NSFontAttributeName:[UIFont systemFontOfSize:20],

                              NSForegroundColorAttributeName:[UIColor redColor]

                              

                              };

        

        [str drawInRect:rect withAttributes:dic];

    }

    - (void)drawImage:(CGContextRef)contextRef{

        UIImage *image = [UIImage imageNamed:@"1.jpg"];

        

        CGRect rect =  CGRectMake(40, 40, 100, 100);

        

        [image drawInRect:rect];

        

        //绘制到指定的矩形中,注意如果大小不合适会会进行拉伸

        //    [image drawInRect:CGRectMake(10, 50, 300, 450)];

        //平铺绘制

        //    [image drawAsPatternInRect:CGRectMake(0, 0, 320, 568)];

    }

    //画图片,聊天表情

    - (void)moveImg:(CGContextRef)cont{

        int lowNum;

        int rowNum;

        for (int i = 1; i<=100; i++) {

          

            rowNum = i%12;

            lowNum = i/12;

            

            

            

            NSString *str = [NSString stringWithFormat:@"%03d.png",i];

            

            UIImage *image = [UIImage imageNamed:str];

            

        

            CGRect rect =  CGRectMake(35*(rowNum-1), 30*(lowNum+1), 20, 20);

            

        

            [image drawInRect:rect];

            }

        }

    //贝塞尔

    - (void)Movebes{

        UIBezierPath *path = [[UIBezierPath alloc]init];

        

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

        

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

        

        [[UIColor redColor]setStroke];

        

        [path setLineWidth:5];

        

        [path stroke];

    }

  • 相关阅读:
    揭秘富人的22种习惯与风格
    CSS
    浏览器--编辑器
    3 位运算 , 补码 ----在开发中比较少用
    2 Java数据类型+转义字符
    1 概述
    如何使用大脑
    JDBC
    web.xml文件的作用
    WindowBuilder插件探索
  • 原文地址:https://www.cnblogs.com/answer-Allen/p/4818371.html
Copyright © 2020-2023  润新知