zoukankan      html  css  js  c++  java
  • Quartz2D

    1->DrawLine
     
    1.获得图形上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    2.设置线段属性

    // 设置线段宽度

     CGContextSetLineWidth(ctx, 10);

     // 设置线段头尾部的样式

     CGContextSetLineCap(ctx, kCGLineCapRound);

     // 设置线段转折点的样式

      CGContextSetLineJoin(ctx, kCGLineJoinRound);

    // 设置颜色

     CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);

    3、画线

    // 设置一个起点

     CGContextMoveToPoint(ctx, 10, 10);

    // 添加一条线段到(100, 100)

     CGContextAddLineToPoint(ctx, 100, 100);

    4、渲染

    CGContextStrokePath(ctx);

    2->DrawShape
    2.1- >画四边形    
        // 1.获得上下文

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // 2.画矩形

        CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));

        //3、设置颜色的几个常用方法

        // set : 同时设置为实心和空心颜色

        // setStroke : 设置空心颜色

        // setFill : 设置实心颜色

        //CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);

        [[UIColor whiteColor] set];

        // 4.绘制图形

        CGContextFillPath(ctx);

    2.2 -> 画圆   

      // 1.获得上下文

      CGContextRef ctx = UIGraphicsGetCurrentContext();

      // 2.画圆

      CGContextAddEllipseInRect(ctx, CGRectMake(50, 10, 100, 100));

       CGContextSetLineWidth(ctx, 10);

       // 3.显示所绘制的东西

        CGContextStrokePath(ctx);

    2.3 ->画圆弧    

       // 1.获得上下文

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // 2.画圆弧

        // xy : 圆心

        // radius : 半径

        // startAngle : 开始角度

        // endAngle : 结束角度

        // clockwise : 圆弧的伸展方向(0:顺时针, 1:逆时针)

        CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);

        // 3.显示所绘制的东西

        CGContextFillPath(ctx);

    2.4 ->画字符串 

        NSString *str = @“为我所画";

        [str drawInRect:CGRectMake(0, 180, 100, 30) withAttributes:nil];

    2.3 ->画图片 

       // 1.取得图片

        UIImage *image = [UIImage imageNamed:@"me"];

        // 2.画

        //[image drawAtPoint:CGPointMake(50, 50)];

        //[image drawInRect:CGRectMake(0, 0, 150, 150)];

        [image drawAsPatternInRect:CGRectMake(0, 0, 200, 200)];

    3->图形上下文栈

       // 1.获得上下文

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        // 将ctx拷贝一份放到栈中

        CGContextSaveGState(ctx);

        // 设置绘图状态

        CGContextSetLineWidth(ctx, 10);

        [[UIColor redColor] set];

        CGContextSetLineCap(ctx, kCGLineCapRound);

        // 第1根线

        CGContextMoveToPoint(ctx, 50, 50);

        CGContextAddLineToPoint(ctx, 120, 190);

        CGContextStrokePath(ctx);

        // 将栈顶的上下文出栈,替换当前的上下文

        CGContextRestoreGState(ctx);

        // 第2根线

        CGContextMoveToPoint(ctx, 10, 70);

        CGContextAddLineToPoint(ctx, 220, 290);

        //CGContextDrawPath(ctx, kCGPathStroke);

        CGContextStrokePath(ctx);    

      

    4->矩阵操作

       //取出上下文

        CGContextRef ctx = UIGraphicsGetCurrentContext();

        //保存上下文到上下文栈中

        CGContextSaveGState(ctx);

        // 矩阵操作(对上下文进行操作)

        CGContextRotateCTM(ctx, M_PI_4 * 0.3);

        CGContextScaleCTM(ctx, 0.5, 0.5);

        CGContextTranslateCTM(ctx, 0, 150);

        CGContextAddRect(ctx, CGRectMake(10, 10, 50, 50));

        CGContextStrokePath(ctx);

        //将栈顶上下文取出替换当前上下文栈

        CGContextRestoreGState(ctx);

        //画圆

        CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 100, 100));

        CGContextMoveToPoint(ctx, 100, 100);

        CGContextAddLineToPoint(ctx, 200, 250);

        CGContextStrokePath(ctx);

    5->裁剪

        //1、获取上下文

      CGContextRef ctx = UIGraphicsGetCurrentContext();

        CGContextSaveGState(ctx);

        // 2、画圆

        CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));

        // 3、裁剪上下文

        CGContextClip(ctx);

        CGContextFillPath(ctx);

        // 1.显示图片

        UIImage *image = [UIImage imageNamed:@"me"];

        [image drawAtPoint:CGPointMake(100, 100)];

        //将栈顶上下文取出替换当前上下文栈

        CGContextRestoreGState(ctx);

        CGContextAddRect(ctx, CGRectMake(0, 0, 50, 50));

        CGContextFillPath(ctx);

    6->动画(利用CADisplayLink+ setNeedsDisplay)

    6.1 -> CADisplayLink

    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];

        [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

    6.2 -> setNeedsDisplay

    - (void)drawRect:(CGRect)rect

    {

    self.snowY += 5;

     if (self.snowY >= rect.size.height) {

         self.snowY = -100;

        }

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

      [image drawAtPoint:CGPointMake(0, self.snowY)];

     }

     
  • 相关阅读:
    androidimage: load large bitmap Efficiently
    display log information in terminal
    黑客人物介绍
    Linux 基础入门学习
    网络攻防 第四周学习总结
    网络攻防 第二周学习总结
    网络攻防 第三周学习总结
    SpringBoot入门Demo(Hello Word Boot)
    jquery的json对象与字符串之间转换
    Intellij IDEA导入web项目详解(解决访问的404)
  • 原文地址:https://www.cnblogs.com/bluceZ/p/3938379.html
Copyright © 2011-2022 走看看