基本绘图

// 1. 获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

#pragma mark 绘制直线

- (void)drawLine
{
    // 提示,使用Ref声明的对象,不需要用*
    // 1. 获取上下文-UIView对应的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 2. 创建可变的路径并设置路径
    /**
     注意:当我们开发动画的时候,通常需要指定对象运动的路线,然后由动画方法负责实现动画效果
     
     因此,在动画开发中,需要熟练使用路径
     */
    CGMutablePathRef path = CGPathCreateMutable();
    // 划线->
    // 1) 设置起始点
    CGPathMoveToPoint(path, NULL, 50, 50);
    // 2) 设置目标点
    CGPathAddLineToPoint(path, NULL, 200, 200);
    CGPathAddLineToPoint(path, NULL, 50, 200);
    
    // 3) 封闭路径
    // a) 直接指定目标点
//    CGPathAddLineToPoint(path, NULL, 50, 50);
    // b) 使用关闭路径方法
    CGPathCloseSubpath(path);
    
    // 3. 将路径添加到上下文
    CGContextAddPath(context, path);
    
    // 4. 设置上下文属性
    /*
     设置线条颜色
     red  0 ~ 1.0  red / 255
     green 0 ~ 1.0 green / 255
     blue 0 ~ 1.0 blue / 255
     alpha 透明度 0 ~ 1.0 
        0 - 完全透明
        1.0 - 完全不透名
     
     提示:
        1) 在使用rgb颜色设置时,最好不要同时指定 rgb 和 alpha,否则会对性能造成一定影响
        2) 默认线条和填充颜色都是黑色
     */
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
    
    // 设置线条宽度
    CGContextSetLineWidth(context, 5.0);
    // 设置线条的顶点样式
    CGContextSetLineCap(context, kCGLineCapRound);
    // 设置线条的连接点样式
    CGContextSetLineJoin(context, kCGLineJoinRound);
    // 设置线条的虚线样式
    /**
     虚线的参数
     
     context
     phase 相位,虚线起始的位置,通常使用0即可,从头开始画虚线
     lengths 长度的数组
     count lengths数组的个数
     */
    CGFloat lengths[2] = {20.0, 10.0};
    CGContextSetLineDash(context, 0.0, lengths, 2);
    
    // 5. 绘制路径
    /**
     kCGPathStroke: 画线(空心)
     kCGPathFill:   填充(实心)
     kCGPathFillStroke: 即画线又填充
     */
    CGContextDrawPath(context, kCGPathFillStroke);
    
    // 6. 释放路径
    CGPathRelease(path);
}

#pragma mark 绘制直线2 - 使用默认的context进行绘图

- (void)drawLine2
{
    // 1. 获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 2. 设置当前上下文的路径
    // 1) 设置起始点
    CGContextMoveToPoint(context, 50, 50);
    // 2) 增加点
    CGContextAddLineToPoint(context, 200, 200);
    CGContextAddLineToPoint(context, 50, 200);
    // 3) 关闭路径
    CGContextClosePath(context);
    
    // 3 设置属性
    /*
     UIKit默认会导入Core Graphics框架,UIKit对常用的很多CG方法做了封装
     
     UIColor setStroke  设置边线颜色
     UIColor setFill    设置填充颜色
     UIColor set        设置边线和填充颜色
     */
    // 设置边线
//    [[UIColor redColor]setStroke];
    // 设置填充
//    [[UIColor blueColor]setFill];
    // 设置边线和填充
    [[UIColor greenColor]set];
    
    // 4 绘制路径,虽然没有直接定义路径,但是第2步操作,就是为上下文指定路径
    CGContextDrawPath(context, kCGPathFillStroke);
}

#pragma mark 绘制矩形

- (void)drawShapeRect
{
    /**
     在程序开发中,无论肉眼看到的是什么形状的对象,其本质都是矩形的
     */
    CGRect rect = CGRectMake(50, 50, 200.0, 200.0);
    
    [[UIColor redColor]set];
    
    // 绘制实心矩形
    UIRectFill(rect);
    // 绘制空心矩形
    UIRectFrame(CGRectMake(50, 300, 100, 100));
}

#pragma mark 绘制圆形

- (void)drawShapeCicle
{
    // 1. 取出上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGRect rect = CGRectMake(50, 50, 200, 100);
    
    // 2. 设置路径
    UIRectFrame(rect);
    CGContextAddEllipseInRect(context, rect);
    
    // 3. 绘制路径
    CGContextDrawPath(context, kCGPathFillStroke);
}

#pragma mark 绘制圆弧

- (void)drawArc:(CGContextRef)context
{
    // 1. 设置路径
    /**
     1) context 上下文
     2) x,y 是圆弧所在圆的中心点坐标
     3) radius 半径,所在圆的半径
     4) startAngle endAngle 起始角度和截止角度 单位是弧度
        0度 对应是圆的最右侧点
     5) clockwise 顺时针 0 或者逆时针 1
     */
    CGContextAddArc(context, 160, 230, 100, -M_PI_4, M_PI_4, 0);
    
    // 2. 绘制圆弧
    CGContextDrawPath(context, kCGPathFill);
}

#pragma mark 绘制文字

- (void)drawText:(CGContextRef)context
{
    NSString *string = @"Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world ";
    
    // 1. 获取字体
    NSLog(@"%@", [UIFont familyNames]);
    UIFont *font = [UIFont fontWithName:@"Marker Felt" size:20];
    // 在指定点绘制字符串
//    [string drawAtPoint:CGPointMake(50, 50) withFont:font];
    // 如果在UILabel中,可以将numbersOfLine设置为0,并且指定足够的高度即可
    CGRect rect = CGRectMake(50, 50, 210, 360);
    [[UIColor lightGrayColor]set];
    UIRectFill(rect);
    
    [[UIColor redColor]set];
    /*
     在英文中通常使用按照单词换行的方式处理 NSLineBreakByWordWrapping
     
     提示,在对齐方式的枚举中
        a. NSTextAlignmentJustified 两端对齐
        b. NSTextAlignmentNatural
     以上两种对齐方式不能使用
     */
    [string drawInRect:rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
}

#pragma mark 绘制文字(中文竖排)

- (void)drawText2:(CGContextRef)context
{
    NSString *string = @"般若波罗蜜多心经";
    
    // 1. 获取字体
    NSLog(@"%@", [UIFont familyNames]);
    UIFont *font = [UIFont fontWithName:@"Marker Felt" size:20];
    // 在指定点绘制字符串
    //    [string drawAtPoint:CGPointMake(50, 50) withFont:font];
    // 如果在UILabel中,可以将numbersOfLine设置为0,并且指定足够的高度即可
    CGRect rect = CGRectMake(290, 10, 30, 440);
    [[UIColor lightGrayColor]set];
    UIRectFill(rect);
    
    [[UIColor redColor]set];
    /*
     在英文中通常使用按照单词换行的方式处理 NSLineBreakByWordWrapping
     
     提示,在对齐方式的枚举中
     a. NSTextAlignmentJustified 两端对齐
     b. NSTextAlignmentNatural
     以上两种对齐方式不能使用
     */
    [string drawInRect:rect withFont:font lineBreakMode:NSLineBreakByCharWrapping alignment:NSTextAlignmentCenter];
}

#pragma mark 绘制图像

- (void)drawImage:(CGContextRef)context
{
    UIImage *image = [UIImage imageNamed:@"头像1.png"];
    
    // 提示:绘制之后,就无法改变位置,也没有办法监听手势识别
    // 在指定点绘制图像
    [image drawAtPoint:CGPointMake(50, 50)];
    
    // 会在指定的矩形中拉伸绘制
    [image drawInRect:CGRectMake(0, 0, 320, 460)];
    
    // 在指定矩形区域中平铺图片
    [image drawAsPatternInRect:CGRectMake(0, 0, 320, 460)];
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值