zoukankan      html  css  js  c++  java
  • CoreGraphics--画线/圆/矩形

    - (void)drawRect:(CGRect)rect {

        // Drawing code

        NSLog(@"drawRect自动调用");

        //画图步骤

        

    //获取上下文(/画笔/绘图环境)

        CGContextRef context = UIGraphicsGetCurrentContext();

        //设置画笔颜色

        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

        //线条的宽度

        CGContextSetLineWidth(context, 4);

        

        //开始画

        //1.画一条直线

    //    [self drawOneLineWith:context];

        

        //2-1.画多条线

    //    [self drawManyLines:context];

        

        //2-2

    //    [self drawManyLines2:context];

        

        //3画虚线

    //    [self drawDashLine:context];

        

        //4 画矩形

    //    [self drawRectShape:context];

        

        //5.填充的矩形

    //    [self drawFillRect:context];

        

        //6 画椭圆

    //    [self drawCircle:context];

        //7.填充的椭圆

    //    [self drawFillCircle:context];

        

        //8 显示文字

    //    [self drawText];

        

        //9.显示图片

        [self drawImage];

        // 10绘制曲线

     

      //11圆弧

     

    }

    #pragmark mark  11  圆弧 

    - (void)drawArc{

        //画布

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        CGContextBeginPath(ctx);

    //圆弧

    *  @param c          当前图形

    *  @param x          圆弧的中心点坐标x

    *  @param y          曲线控制点的y坐标

    *  @param radius      指定点的x坐标值

    *  @param startAngle  弧的起点与正X轴的夹角,

    *  @param endAngle    弧的终点与正X轴的夹角

    *  @param clockwise  指定1创建一个顺时针的圆弧,或是指定0创建一个逆时针圆弧

    *

    */

        CGContextAddArc(ctx, 100, 100, 50, 0,M_PI/2, 0);

        CGContextSetLineWidth(ctx, 3); //线宽

    //    CGContextStrokePath(ctx);//描线

        CGContextFillPath(ctx);  //填充

    }

     

    #pragma mark   10  二次曲线

    - (void)drawCurve {

    // 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 创建一个新的空图形路径。

    CGContextBeginPath(ctx);

    //开始点

    CGContextMoveToPoint(ctx, 160, 100);

    /** * @brief 在指定点追加二次贝塞尔曲线,通过控制点和结束点指定曲线。 

    @param c 当前图形 * @param cpx 曲线控制点的x坐标 *

    @param cpy 曲线控制点的y坐标 *

    @param x 指定点的x坐标值 *

    @param y 指定点的y坐标值 * */

    CGContextAddQuadCurveToPoint(ctx, 160, 50, 190, 50);


    CGContextSetLineWidth(ctx, 20);

    CGContextSetStrokeColorWithColor(ctx, [UIColor brownColor].CGColor);

    CGContextStrokePath(ctx);

    }

     

    #pragma makr -- 9 显示图片

    - (void)drawImage{

        

        [[UIImage imageNamed:@"收藏-128"] drawInRect:CGRectMake(0, 0, 100, 100)];

        

    }

     

     

    #pragma mark -- 8 显示文字

    - (void)drawText{

        //起点(attribute 中设这参数)

        [@"aaabbbcccddd" drawAtPoint:CGPointMake(100, 100) withAttributes:NULL];

        //

        [@"asdfasdfaf" drawInRect:CGRectMake(100, 150, 200, 200) withAttributes:NULL];

        

        

    }

     

    #pragma mark -- 7 画填充的 椭圆

    - (void)drawFillCircle:(CGContextRef)context{

        

        //填充的颜色

        [[UIColor redColor] setFill];

        CGContextFillEllipseInRect(context, CGRectMake(40, 40, 200, 300));

        

     

        

    }

     

    #pragma mark -- 6 画椭圆

    - (void)drawCircle:(CGContextRef)context{

        

        CGContextStrokeEllipseInRect(context, CGRectMake(40, 40, 300, 200));

        

    }

     

    #pragma mark -- 5填充的矩形

    - (void)drawFillRect:(CGContextRef)context{

        

        //设置填充颜色

        [[UIColor yellowColor] setFill];

        CGContextFillRect(context, CGRectMake(40, 40, 250, 300));

     

        

    }

     

    #pragma mark --4画矩形

    - (void)drawRectShape:(CGContextRef)context{

        

        CGContextStrokeRect(context, CGRectMake(40, 40, 200, 300));

        

        //同理(stroke 画图方法; 也有add..方法 )

    //    CGContextAddRect(context,  CGRectMake(40, 40, 200, 300));

    //    CGContextStrokePath(context);

        

    }

     

     

    #pragma mark -- 3 画虚线

    - (void)drawDashLine:(CGContextRef)context{

        

        //设置虚线类型

    //    CGFloat lengths[] = {5,15,5}; //5 空隙15 5 ...

        CGFloat lengths[] = {5,5};

        CGContextSetLineDash(context, 0, lengths, sizeof(lengths)/sizeof(lengths[0]));

        

    //    CGContextSetLineDash(<#CGContextRef c#>, <#CGFloat phase#>, <#const CGFloat *lengths#>, <#size_t count#>)

        //phase 开始点 跳过多少个点数(0代表从头画)

        //lengths 虚线的样式

        //count 长度

        

        //起点

        CGContextMoveToPoint(context, 50, 50);

        //画线

        CGContextAddLineToPoint(context, 200, 200);

        

        CGContextStrokePath(context);

        

    //第二段设置成  直线 NULL

        CGContextSetLineDash(context, 0, NULL, 0);

        //需要重新设置起点

        CGContextMoveToPoint(context, 200, 200);

        

        CGContextAddLineToPoint(context, 250,400);

     

        CGContextStrokePath(context);

        

    }

     

     

     

    #pragma mark --画多条线 (方式二)

    - (void)drawManyLines2:(CGContextRef)context{

        

        CGPoint point[] = {CGPointMake(50, 50),CGPointMake(100, 100),CGPointMake(100, 200)};

        //计算数组中元素个数

    //    int count  = sizeof(数组名)/size(sizeof(a[0]));

        

                                    //个数

        CGContextAddLines(context, point, sizeof(point)/sizeof(point[0]));

        //闭合

        CGContextClosePath(context);

        //开始画

        CGContextStrokePath(context);

        

        

    }

     

     

     

    #pragma mark --画多条线 (方式一)

    - (void)drawManyLines:(CGContextRef)context{

       

        //例如:三角形

        CGContextMoveToPoint(context, 200, 100);

        

        CGContextAddLineToPoint(context, 100, 200);

        CGContextAddLineToPoint(context, 300, 200);

        //回到起点

    //    CGContextAddLineToPoint(context, 200, 100);

        //按方法2 : 闭合(起点和终点连线)

        CGContextClosePath(context);

        

        CGContextStrokePath(context);

        

    }

     

     

    #pragma mark -- 1 画一条线

    - (void)drawOneLineWith:(CGContextRef)context{

        //起点

        CGContextMoveToPoint(context, 50, 50);

        //终点:画线

        CGContextAddLineToPoint(context, 200, 200);

        //开始画

        CGContextStrokePath(context); //画线

      //CGContextFillPath(context);   //填充路径

        

    }

  • 相关阅读:
    ArcGIS for Android地图控件的5大常见操作
    adb开启不了解决方案
    Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤
    解决 Your project contains error(s),please fix them before running your application问题
    二路归并算法实现
    字符串全排列
    python连接MySQL
    .net常考面试题
    win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面
    int.Parse()与int.TryParse()
  • 原文地址:https://www.cnblogs.com/daxueshan/p/5816269.html
Copyright © 2011-2022 走看看