• iOS-Quartz 2D


      1 /*
      2  1.     使用Quartz 2D绘图的基本步骤
      3  
      4  1) 获取上下文context(绘制图形的地方)
      5  2) 设置路径(路径是用来描述形状的)
      6  3)  将路径添加到上下文
      7  4)  设置上下文属性(设置颜色,线宽,线性等)
      8  5)  绘制路径
      9  6)  释放路径(在Quartz2D中,因为是C语言的框架,所有用Create,Copy字样方法实例化的对象,都需要自行释放)
     10  
     11  2. Quartz2D是一套C语言的框架,C语言中使用方法
     12  
     13  1> 在C语言中,所有的对象,都是通过对象的引用地址来使用的,因此不需要使用*
     14  2> C语言不是面向对象的语言,它是面向过程的语言,因此,在C语言中,如果要操作某一个对象,都是通过函数来实现
     15  3> ARC是OC的自动引用计数,如果在C语言中,为对象分配(Create,Copy)了内容,我们需要自己释放(Release)
     16  
     17  3. 设置RGB颜色的介绍
     18  
     19  Red    红(0~255)程序员(0~FF) => r / 255.0 0~1.0
     20  Green  绿(0~255)程序员(0~FF) => g / 255.0 0~1.0
     21  Blue   蓝(0~255)程序员(0~FF) => b / 255.0 0~1.0
     22  Alpha  透明度 0~1.0
     23  0表示完全透明
     24  1表示完全不透明
     25  
     26  2^8 * 2^8 * 2^8 = 2^24 = 2^4 * 2^10 * 2^10 = 16 * 1K * 1K 16位真彩色
     27  
     28  提示:仅在使用Quartz2D绘图时,尽量不要改变Alpha值,如果改变透明度,会严重影响性能
     29  
     30  如果不指定颜色,默认使用黑色
     31  
     32  4. 填充模式
     33  
     34  kCGPathFill            填充绘制,针对关闭的路径使用
     35  kCGPathStroke          绘制线条,无所谓路径是否关闭
     36  kCGPathFillStroke      填充并绘制线条
     37  kCGPathEOFill          异或填充,针对关闭路径使用
     38  kCGPathEOFillStroke    异或填充并画线
     39  */
     40 // Only override drawRect: if you perform custom drawing.
     41 // An empty implementation adversely affects performance during animation.
     42 - (void)drawRect:(CGRect)rect
     43 {
     44     [self drawText];
     45 }
     46 
     47 #pragma mark - 绘制形状方法
     48 #pragma mark 绘制文本
     49 - (void)drawText
     50 {
     51     NSString *text = @"床前明月光,疑是地上霜!";
     52 
     53     UIFont *font = [UIFont systemFontOfSize:17];
     54     NSDictionary *dict = @{NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor redColor]};
     55     
     56     // 以下方法在iOS7被废弃
     57 //    [text sizeWithFont:font constrainedToSize:CGSizeMake(100, 10000) lineBreakMode:NSLineBreakByCharWrapping];
     58     CGRect rect = [text boundingRectWithSize:CGSizeMake(20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
     59     
     60     rect.origin = CGPointMake(100, 20);
     61 
     62     // 给文本加一个背景颜色
     63     [[UIColor lightGrayColor] set];
     64     UIRectFill(rect);
     65     
     66     // 在iOS7中,所有文本绘制属性,均需要使用属性字典进行设置!
     67 //    [[UIColor redColor] set];
     68     [text drawInRect:rect withAttributes:dict];
     69 }
     70 
     71 #pragma mark 绘制图像
     72 - (void)drawImage
     73 {
     74     // 提示:绘制的图像,只能看不能交互,相对而言,性能比UIImageView要高
     75     UIImage *image = [UIImage imageNamed:@"头像1"];
     76     
     77     // 1. 在指定坐标绘制
     78 //    [image drawAtPoint:CGPointMake(80, 80)];
     79     
     80     // 2. 在指定区域内拉伸绘制
     81 //    [image drawInRect:CGRectMake(100, 100, 150, 250)];
     82     
     83     // 3. 平铺绘制
     84     [image drawAsPatternInRect:self.bounds];
     85 }
     86 
     87 #pragma mark 绘制弧线
     88 - (void)drawArc
     89 {
     90     CGContextRef context = UIGraphicsGetCurrentContext();
     91     
     92     /**
     93      弧线参数:
     94      
     95      1)上下文
     96      2) x, y 弧线所在圆上的圆心
     97      3)半径
     98      4) 开始角度、结束角度,0度是圆形最右侧的点
     99      5) 顺时针:0 逆时针:1
    100      */
    101     CGContextAddArc(context, 160, 240, 100, 0, -M_PI_2, 0);
    102     
    103     // 3. 绘制圆形
    104     CGContextDrawPath(context, kCGPathFill);
    105 }
    106 
    107 #pragma mark 绘制圆形
    108 - (void)drawCicle
    109 {
    110     CGContextRef context = UIGraphicsGetCurrentContext();
    111     
    112     // 在iOS中,如果要绘制圆形,需要先指定一个矩形
    113     // 绘制出来的圆形,是内切于该矩形的
    114     
    115     // 1. 指定绘制圆形外切的矩形
    116     CGRect rect = CGRectMake(100, 100, 150, 150);
    117     
    118     // 2. 添加圆形到上下文路径
    119     CGContextAddEllipseInRect(context, rect);
    120     
    121     // 3. 绘制圆形
    122     CGContextDrawPath(context, kCGPathFill);
    123 }
    124 
    125 #pragma mark 绘制矩形
    126 - (void)drawRectangle
    127 {
    128     CGRect rect = CGRectMake(100, 100, 200, 200);
    129     
    130     [[UIColor redColor] set];
    131     // 绘制实心矩形
    132     UIRectFill(rect);
    133     
    134     CGRect rect1 = CGRectMake(150, 150, 50, 50);
    135     
    136     [[UIColor blueColor] set];
    137     // 绘制空心举行
    138     UIRectFrame(rect1);
    139 }
    140 
    141 #pragma mark 利用上下文直接绘制直线
    142 - (void)drawLineWithContext
    143 {
    144     /**
    145      利用上下文直接绘图,比使用路径要简单
    146      
    147      1> 使用上下文直接绘图,意味着每次都要指定其中的路径才能够绘制
    148      2> 使用路径绘图,意味着可以预定义一组路径,在需要时直接绘制,灵活度高
    149      3> 除了绘制图形之外,路径还可以用在核心动画中,指定视图运动的轨迹
    150      
    151      关于路径,无论是使用绘图,还是核心动画,都必须要掌握
    152      */
    153     // 1. 获取上下文
    154     CGContextRef context = UIGraphicsGetCurrentContext();
    155     
    156     // 2. 设置上下文中的路径
    157     CGContextMoveToPoint(context, 100, 100);
    158     CGContextAddLineToPoint(context, 200, 200);
    159     CGContextAddLineToPoint(context, 100, 200);
    160 //    CGContextAddLineToPoint(context, 100, 100);
    161     // 关闭路径
    162     CGContextClosePath(context);
    163     
    164     // 3. 设置上下文属性
    165     /**
    166      因为核心图形使用非常频繁,因此UIKit对于常用的绘图做了一定的封装
    167      
    168      设置边线颜色
    169      [[UIColor redColor] setStroke];
    170      // 设置填充颜色
    171      [[UIColor blueColor] setFill];
    172      // 同时设置边线和填充颜色
    173      [[UIColor greenColor] set];
    174      
    175      */
    176     // 设置边线颜色
    177     [[UIColor redColor] setStroke];
    178     // 设置填充颜色
    179     [[UIColor blueColor] setFill];
    180     // 同时设置边线和填充颜色
    181     [[UIColor greenColor] set];
    182     
    183     // 4. 绘制上下文
    184     CGContextDrawPath(context, kCGPathFillStroke);
    185 }
    186 
    187 #pragma mark 绘制一条直线
    188 - (void)drawLine
    189 {
    190     // 1. 获取当前图形上下文,就是要绘制到屏幕
    191     CGContextRef context = UIGraphicsGetCurrentContext();
    192     
    193     // 2. 设置路径
    194     CGMutablePathRef path = CGPathCreateMutable();
    195     // 1) 移动到某一个点
    196     CGPathMoveToPoint(path, NULL, 100, 100);
    197     // 2) 增加一条直线
    198     CGPathAddLineToPoint(path, NULL, 200, 200);
    199     
    200     // 3. 将路径添加到上下文
    201     CGContextAddPath(context, path);
    202     
    203     // 4. 设置上下文
    204     // 1) 设置线条颜色
    205     CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    206     
    207     // 5. 将上下文中的路径绘制出来
    208     CGContextDrawPath(context, kCGPathStroke);
    209     
    210     // 6. 释放路径
    211     CGPathRelease(path);
    212 }
  • 相关阅读:
    C++的XML编程经验――LIBXML2库使用指南
    C/C++:sizeof('a')的值为什么不一样?
    Linux core dump file详解
    非阻塞socket的连接
    Java环境设置、HelloWorld例子、Ant环境及运行
    linux下杀死进程命令
    IP协议详解
    内置函数(上)
    异常处理
    递归函数与二分法
  • 原文地址:https://www.cnblogs.com/DarbyCJ/p/3673362.html
Copyright © 2020-2023  润新知