iOS

Quartz2D 基础

2019-06-30  本文已影响0人  习惯了_就好
void drawLine(){
    NSLog(@"--------------------------------画线段--------------------------------");
    //获取图形上下文对象
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置起点
    CGContextMoveToPoint(context, 10, 10);
    //添加线段至某一点
    CGContextAddLineToPoint(context, 100, 100);
    //设置颜色
    [[UIColor redColor] setStroke];
    //设置线的粗细
    CGContextSetLineWidth(context, 10);
    //设置线的端点形状
    CGContextSetLineCap(context, kCGLineCapRound);
    //渲染显示到view上
    CGContextStrokePath(context);
}

void drawTriangle(){
    NSLog(@"--------------------------------画三角--------------------------------");
    //获取图形上下文对象
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置起点
    CGContextMoveToPoint(context, 10, 10);
    //添加线段至某一点
    CGContextAddLineToPoint(context, 100, 100);
    
    CGContextAddLineToPoint(context, 150, 50);
    
    CGContextSetLineWidth(context, 10);
    
    //
    [[UIColor redColor] setFill];
    
    //封闭路径
    CGContextClosePath(context);
    
    //渲染显示到view上
    CGContextFillPath(context);
    
    
    [[UIColor blueColor] setStroke];
    CGContextStrokePath(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);
}
上一篇下一篇

猜你喜欢

热点阅读