Quartz2D 基础

void drawLine(){
            NSLog(@"--------------------------------画线段--------------------------------");
    //获取图形上下文对象
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //将当前上下文对象copy一份,放到图形上下文栈的栈顶
    CGContextSaveGState(context);
    
    
    //设置起点
    CGContextMoveToPoint(context, 10, 10);
    //添加线段至某一点
    CGContextAddLineToPoint(context, 100, 100);
    //设置颜色
    [[UIColor redColor] setStroke];
    //设置线的粗细
    CGContextSetLineWidth(context, 10);
    //设置线的端点形状
    CGContextSetLineCap(context, kCGLineCapRound);
    //渲染显示到view上
    CGContextStrokePath(context);
    
    
    //将上面存储到上下文栈里面的上下文对象取出来,用它t去替换上面设置属性的上下文
    CGContextRestoreGState(context);
    
    //这里的上下文是从栈里新取出来的,上面设置的属性对这里无效
    CGContextMoveToPoint(context, 15, 5);
    CGContextAddLineToPoint(context, 120, 40);
    
    CGFloat lengths[] = {5,10}; //分别是点的长度,空白的长度
    //第二个参数phase参数表示在第一个虚线绘制的时候跳过多少个点;第三个参数lengths表示点和空白的长度;第四个参数count,是lengths数组的长度
    CGContextSetLineDash(context, 0, lengths,2);
    CGContextStrokePath(context);

}

void drawTriangle(){
    NSLog(@"--------------------------------画三角--------------------------------");
    //获取图形上下文对象
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    //设置起点
    CGContextMoveToPoint(context, 10, 10);
    //添加线段至某一点
    CGContextAddLineToPoint(context, 100, 100);
    CGContextAddLineToPoint(context, 150, 50);
    //
    [[UIColor redColor] setFill];
    //封闭路径
    CGContextClosePath(context);
    //渲染显示到view上
    CGContextFillPath(context);
    CGContextRestoreGState(context);
    
    //平移
    CGContextTranslateCTM(context, 0, 50);
    //缩放
    CGContextScaleCTM(context, 1.5, 0.5);
//    旋转
    CGContextRotateCTM(context, M_PI * 0.2);
    //设置起点
    CGContextMoveToPoint(context, 10, 10);
    //添加线段至某一点
    CGContextAddLineToPoint(context, 100, 100);
    CGContextAddLineToPoint(context, 150, 50);
    //阴影,offset:x轴和y轴偏移的距离,blue模糊程度
    CGContextSetShadow(context, CGSizeMake(100, 5), 5);
    [[UIColor blueColor] setFill];
    //封闭路径
    CGContextClosePath(context);
    //渲染显示到view上
    CGContextFillPath(context);
}

void drar4Rect()
{
    NSLog(@"--------------------------------画四边形--------------------------------");
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddRect(context, CGRectMake(10, 10, 30, 100));
    
//    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineWidth(context, 10);
    [[UIColor blueColor] setStroke];
    CGContextStrokePath(context);

    
//    CGContextAddRect(context, CGRectMake(10, 10, 30, 100));
//    [[UIColor redColor] setFill];
//    CGContextFillPath(context);
    
}

void drawArc()
{
    NSLog(@"--------------------------------画圆弧--------------------------------");
    CGContextRef context = UIGraphicsGetCurrentContext();
    /**
     上下文
     x坐标
     y坐标
     半径
     起始角度
     结束角度
     0顺时针  1逆时针
     
     x轴正向为0度,y轴x向下为90度,x轴反向为180度,y轴向上为270度
     */
    CGContextAddArc(context, 100, 100, 20, 0, M_PI_2, 0);
    CGContextStrokePath(context);
}

void drawCircle()
{
    NSLog(@"--------------------------------画圆--------------------------------");
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(context, CGRectMake(100, 100, 60, 60));
    CGContextStrokePath(context);
}

void drawsquCircle(){
    NSLog(@"--------------------------------画1/4圆--------------------------------");
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(context, 100, 100);
    CGContextAddLineToPoint(context, 100, 130);
    CGContextAddArc(context, 100, 100, 30, M_PI_2, 0, 1);
    CGContextClosePath(context);
    //画边
    CGContextStrokePath(context);
    //画内部
//    CGContextFillPath(context);
}

void drawText(){
    NSLog(@"--------------------------------画文本-------------------------------");
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(20, 20, 100, 100);
    CGContextAddRect(context, rect);
    CGContextFillPath(context);
    
    NSString * str = @"画出来点文本";
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    //设置文本字体大小
    dic[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    //设置文本颜色
    dic[NSForegroundColorAttributeName] = [UIColor redColor];
    //在矩形范围内画文本
    [str drawInRect:rect withAttributes:dic];
}

void drawImage(){
    NSLog(@"--------------------------------画图片-------------------------------");
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, 240, 240);
    [[UIColor redColor] set];
    CGContextAddRect(context, rect);
    CGContextFillPath(context);
    
    UIImage * image = [UIImage imageNamed:@"me"];
    
//    绘制一次,按矩形大小去绘制图片
//    [image drawInRect:rect];
    //重复绘制
    [image drawAsPatternInRect:rect];
}

void drawTextAndImage(){
    NSLog(@"--------------------------------画图片和文本-------------------------------");
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(5, 5, 200, 200);
    CGContextAddRect(context, rect);
    CGContextFillPath(context);
    
    //画图片
    UIImage * image = [UIImage imageNamed:@"me"];
    [image drawInRect:rect];
    
    //画文本
    NSString * str = @"画出来点文本";
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    //设置文本字体大小
    dic[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    //设置文本颜色
    dic[NSForegroundColorAttributeName] = [UIColor redColor];
    //在矩形范围内画文本
//    [str drawInRect:rect withAttributes:dic];
    [str drawInRect:CGRectMake(150 , 150, 200, 200) withAttributes:dic];
    
}

void drawQuadCurve(CGContextRef context ,CGRect rect){
    NSLog(@"--------------------------------画贝塞尔曲线-------------------------------");
    //控制点
    CGFloat controlX = rect.size.width * 0.5;
    CGFloat controlY = rect.size.height * 0.4;
    
    //开始点
    CGFloat leftMargin = 30;
    CGFloat startX = controlX - leftMargin;
    CGFloat startY = controlY - 20;
    CGContextMoveToPoint(context, startX, startY);
    
    //结束点
    CGFloat endX = controlX + leftMargin;
    CGFloat endY = startY;
    
    //画贝塞尔曲线
    CGContextAddQuadCurveToPoint(context, controlX, controlY, endX, endY);
    
    //
    CGContextStrokePath(context);
}

void drawClip(){
    NSLog(@"--------------------------------裁剪-------------------------------");
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGRect rect = CGRectMake(10, 10, 50, 50);
    CGContextAddEllipseInRect(context, rect);
    //设置裁剪
    CGContextClip(context);
    CGContextFillPath(context);

    
    UIImage * image = [UIImage imageNamed:@"me"];
    [image drawAtPoint:CGPointMake(10, 10)];
    
}

void drawWithPath(){
    NSLog(@"--------------------------------使用path画图形-------------------------------");
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 10, 10);
    CGPathAddLineToPoint(path, NULL, 150, 100);
    CGContextAddPath(context, path);
    
    CGContextStrokePath(context);
    //释放
    CGPathRelease(path);
}

你可能感兴趣的:(Quartz2D 基础)