zoukankan      html  css  js  c++  java
  • iOS开发之Quartz2D 二:绘制直线,曲线,圆弧,矩形,椭圆,圆

    #import "DrawView.h"
    
    @implementation DrawView
    
    
    /**
     *  作用:专门用来绘图
     *  什么时候调用:当View显示的时候调用
     *  @param rect:当View的bounds
     */
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    //    NSLog(@"%s",__func__);
    //    NSLog(@"%@",NSStringFromCGRect(rect));
        
        //1.在drawRect方法当中系统已经帮你创建一个跟View相关联的上下文.(Layer上下文)
        //只要获取上下文就行了.
        
        //绘制曲线
        //1.获取上下文
        CGContextRef ctx =  UIGraphicsGetCurrentContext();
        //2.描述路径
        UIBezierPath *path = [UIBezierPath bezierPath];
        //画曲线
        //2.1设置起点
        [path moveToPoint:CGPointMake(50, 280)];
        //2.2添加一根曲线到某一个点
        [path addQuadCurveToPoint:CGPointMake(250, 280) controlPoint:CGPointMake(250, 200)];
        //3.把路径添加到上下文
        CGContextAddPath(ctx, path.CGPath);
        //4.把上下文的内容渲染View上
        CGContextStrokePath(ctx);
    
    }
    
    //画直线
    - (void)drawLine{
    
        
        //1.获取上下文(获取,创建上下文 都 以uigraphics开头)
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        //2.绘制路径
        UIBezierPath *path = [UIBezierPath bezierPath];
        //2.1 设置起点
        [path moveToPoint:CGPointMake(50, 280)];
        //2.2 添加一根线到终点
        [path addLineToPoint:CGPointMake(250, 50)];
        
        //画第二条线
        //    [path moveToPoint:CGPointMake(100, 280)];
        //    [path addLineToPoint:CGPointMake(250, 100)];
        
        //addLineToPoint:把上一条线的终点当作是下一条线的起点
        [path addLineToPoint:CGPointMake(200, 280)];
        
        //上下文的状态
        //设置线的宽度
        CGContextSetLineWidth(ctx, 10);
        //设置线的连接样式(两条线的交点为圆角)
        CGContextSetLineJoin(ctx, kCGLineJoinRound);
        //设置线的顶角样式(两根线的两端为圆角)
        CGContextSetLineCap(ctx, kCGLineCapRound);
        
        //设置颜色(此方法会自动判断是stroke描点,还是fill填充而设置颜色)
        [[UIColor redColor] set];
        
        
        
        //3.把绘制的内容添加到上下文当中.
        //UIBezierPath:UIKit框架 ->    CGPathRef:CoreGraphics框架
        CGContextAddPath(ctx, path.CGPath);
        //4.把上下文的内容显示到View上(渲染到View的layer)(stroke fill)
        CGContextStrokePath(ctx);
    
    }
    
    
    
    
    @end
    #import "DrawView.h"
    
    @implementation DrawView
    
    
    
    
    -(void)awakeFromNib{
    
     
        //画椭圆
        //UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 50)];
        
        //使用UIBezierPath提供的绘图方法进行绘制
        //[path stroke]方法中默认做了:1.获取上下文->2.描述路径,3.把路径添加到上下文,4.把上下文的内容渲染View上,只有在drawRect:方法当中才能够获取上下文.在awakeFromNib当获取不到上下文,所以没有办法 进行绘图.
        //[path stroke];
        
    }
    
    
    
    - (void)drawRect:(CGRect)rect {
    
        
    ////        1:绘制矩形 和 直线
    //        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];
    //        [path moveToPoint: CGPointMake(50, 250)];
    //        [path addLineToPoint:CGPointMake(250, 250)];
    //    
    //        [path setLineWidth:10];
    //        [path stroke];
        
        
        
        
    //   2: 使用UIBezierPath提供的绘图方法进行绘制
        //    画椭圆或是圆形:当宽高不相等时时椭圆,相等的时候是圆形,调用UIBezierPath的对象方法[path stroke],默认做了1.获取上下文->2.描述路径,3.把路径添加到上下文,4.把上下文的内容渲染View上,只有在drawRect:方法当中才能够获取上下文.在awakeFromNib当获取不到上下文,所以没有办法 进行绘图.
    
    //    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
    //
    //    [[UIColor redColor] set];
    //    [path stroke];
    
        
        //3:画弧
        //Center:弧所在的圆心
        //radius:圆的半径
        //startAngle:开始角度
        //endAngle:截至角度
        //clockwise: YES:顺时针 NO:逆时针
        
        NSLog(@"%@",NSStringFromCGPoint(self.center));
        
        
        //画弧
    //    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    //    CGFloat radius = rect.size.width * 0.5 - 10;
    //    //不能直接会用self.center ,是因为self.center坐标是相对于它的父控件.
    //    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:-M_PI_2 clockwise:NO];
    //    [path stroke];
        
        
    //    //画扇形
    //    
    //    CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
    //    CGFloat radius = rect.size.width * 0.5 - 10;
    //    //不能直接会用self.center ,是因为self.center坐标是相对于它的父控件.
    //    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:-M_PI_2 clockwise:NO];
    //
    //    //添加一根线到圆心
    //    [path addLineToPoint:center];
    //     [[UIColor redColor] set];
    //    //关闭路径closePath:从路径终点连接一根线到路径的起点
    ////     [path closePath];
    //    
    //   
    //    
    //    //画扇形
    //    //fill(填充之前,会自动关闭路径)
    //   [path fill];
    //
    //    //[path stroke];//1.获取上下文->2.描述路径,3.把路径添加到上下文,4.把上下文的内容渲染View上
    //    
       [self drawRect];
    
    }
    
    
    //画矩形
    - (void)drawRect{
    
        //1.获取上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        //2.描述路径
        //画矩形
        //UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];
        //圆角矩形
        //cornerRadius:圆角半径
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) cornerRadius:23];
        
        [[UIColor yellowColor] set];
        //3.把路径添加到上下文
        CGContextAddPath(ctx, path.CGPath);
        //4.把上下文的内容渲染View上
        //CGContextStrokePath(ctx);一个圆圈
        CGContextFillPath(ctx);//填充
        
    }
    
    @end

    1.DrawRect方法作用?什么时候调用.

    DrawRect作用:专用在这个方法当中绘图的.只有在这个方法当中才能取得跟View相关联的上下文.

    DrawRect是系统自己调用的, 它是当View显示的时候自动调用.

    2.画线(基本步骤描述)

    2.1获取跟View相关联的上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

     

    2.2绘制路径

    UIBezierPath *path = [UIBezierPath bezierPath];

    2.2.1设置起点

        [path moveToPoint:CGPointMake(10, 125)];

       

        2.2.2添加一根线到某个点

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

       

    2.3把路径添加到上下文

    CGContextAddPath(ctx,path.CGPath);

     

    2.4把上下文的内容渲染到View上面.

    CGContextStrokePath(ctx);

     

    3. 想要再添加一根线怎么办?

    第一种方法:重新设置起点,添加一根线到某个点,一个UIBezierPath路径上面可以有多条线.

    第二种方法:直接在原来的基础上添加线.把上一条的终点当做下一条线的起点.添加一根线到某个点

      直接在下面addLineToPoint:

     

    4.怎么样设置线的宽度,颜色,样式?

    设置这些样式,我们称为是修改图形上下文的状态.

    设置线宽:CGContextSetLineWidth(ctx, 20);

    设置线段的连接样式: CGContextSetLineJoin(ctx, kCGLineJoinRound);

    添加顶角样式:CGContextSetLineCap(ctx, kCGLineCapRound);

    设置线的颜色: [[UIColor redColor] set];

     

    5.如何画曲线?

     

    画曲线方法比较特殊需要一个控制点来决定曲线的弯曲程度.画曲线方法为:

    先设置一个曲线的起点

    [path moveToPoint:CGPointMake(10, 125)];

    再添加到个点到曲线的终点.同时还须要一个controlPoint(控件点决定曲线弯曲的方法程序)

            [path addQuadCurveToPoint:CGPointMake(240, 125) controlPoint:CGPointMake(125, 10)];

            

         6.如何画矩形,圆角矩形?

         圆角矩形的画法多了一个参数,cornerRadius

         cornerRadius它是矩形的圆角半径.

         通过圆角矩形可以画一个圆.当矩形是正方形的时候,把圆角半径设为宽度的一半,就是一个圆.

         bezierPathWithRoundedRect:CGRectMake(10, 100, 50, 50) cornerRadius:10

         

         7.如果画椭圆,圆?

          画椭圆的方法为:

         前两个参数分别代码圆的圆心,后面两个参数分别代表圆的宽度,与高度.

         宽高都相等时,画的是一个正圆, 不相等时画的是一个椭圆

         bezierPathWithOvalInRect:CGRectMake(10, 100, 50, 50)

         

    8.如何利用UIKit封装的上下文进行画图?

    直接来个:[path stroke]就可以了.

    它底层的实现,就是获取上下文,拼接路径,把路径添加到上下文,渲染到View

     

    9.如何画圆弧?

     

      首先要确定圆才能确定圆弧,圆孤它就圆上的一个角度嘛

     

      Center:圆心

             radius:圆的半径

             startAngle:起始角度

             endAngle:终点角度

             clockwise:Yes顺时针,No逆时针

         

             注意:startAngle角度的位置是从圆的最右侧为0度.

             

      UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(125, 125)

                        radius:100 

                        startAngle:0 

                        endAngle:M_PI * 2 

                        clockwise:YES];

     

     

    10.如果画扇形.

    画扇形的方法为:先画一个圆孤再添加一个一根线到圆心,然后关闭路径.

      关闭路径就会自动从路径的终点到路径的起点封闭起下

    用填充的话,它会默认做一个封闭路径,从路径的终点到起点. 

    [path fill];

  • 相关阅读:
    Cookie用法
    (转)大型网站架构系列:负载均衡详解(3)
    (转)大型网站架构系列:负载均衡详解(2)
    (转)大型网站架构系列:负载均衡详解(1)
    大型网站架构系列:消息队列
    ASP.NET MVC之从控制器传递数据到视图四种方式
    数据库总结
    C#数字千分位问题
    SQLServer分页存储过程
    查找出‘数据库中的值能匹配输入条件的后几位’的所有记录
  • 原文地址:https://www.cnblogs.com/cqb-learner/p/5820210.html
Copyright © 2011-2022 走看看