01 画简单线的方法

1.画三角形

- (void)drawRect:(CGRect)rect {
    
    // 获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();


// 确定三个点 CGContextMoveToPoint(ctx, 30, 30); // 起点 CGContextAddLineToPoint(ctx, 130, 30); CGContextAddLineToPoint(ctx, 130, 130); // CGContextAddLineToPoint(ctx, 10, 110);
// or
  
   // 关闭路径
    CGContextClosePath(ctx);
//渲染
    CGContextStrokePath(ctx);
}

修饰符

// 设置颜色
    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
    
//    // 设置线宽
    CGContextSetLineWidth(ctx, 20);
//
//    // 设置线的首尾样式
    CGContextSetLineCap(ctx, kCGLineCapButt);  // 和直线感觉差不多
//    CGContextSetLineCap(ctx, kCGLineCapRound); // 两头圆
//    CGContextSetLineCap(ctx, kCGLineCapSquare); // 直线
//
//    //设置线的连接样式
//    CGContextSetLineJoin(ctx, kCGLineJoinRound);  // 连接处圆
    CGContextSetLineJoin(ctx, kCGLineJoinMiter);  // 正常
    CGContextSetLineJoin(ctx, kCGLineJoinBevel);  // 缺角

圆角:    缺角:

 2.画长方形

-(void)drawRectangle{
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //第一种方法
//    CGContextMoveToPoint(ctx, 10, 10);
//    CGContextAddLineToPoint(ctx, 110, 10);
//    CGContextAddLineToPoint(ctx, 110, 110);
//    CGContextAddLineToPoint(ctx, 10, 110);
//    CGContextAddLineToPoint(ctx, 10, 10);
//    CGContextStrokePath(ctx);
    CGContextFillPath(ctx);
    CGContextStrokeRect(ctx, CGRectMake(10, 10, 110, 100));
//    CGContextFillRect(ctx, CGRectMake(10, 10, 110, 100));

    
}

3.画圆弧

-(void)drawArc{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 以此 上下文 x, y, 半径, 开始的位置, 结束的位置, 顺时针还是逆时针,看看提示就都知道了
    CGContextAddArc(context, 100, 100, 50, 0, M_PI_2, 0); 
    CGContextStrokePath(context);
}

 4.画扇形

- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    [self drawArc];
//    [self drawCircle];
    
     // 比圆弧多加一个点
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextMoveToPoint(context, 100, 100);
     CGContextAddArc(context, 100, 100, 50, M_PI_4, 3 * M_PI_4, 0);
     CGContextClosePath(context);
     CGContextStrokePath(context);
}

 5.画圆

 理解成在一个正方形中划出来一个圆就行了。

-(void)drawCircle{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextAddEllipseInRect(context, CGRectMake(10, 10, 100, 100));
    
    CGContextStrokePath(context);
}
原文地址:https://www.cnblogs.com/XXxiaotaiyang/p/5027127.html