iOS画圆、画线

https://blog.csdn.net/csdndimo/article/details/84842727

iOS画圆、画线

UIView:

  1.  
    - (void)drawRect:(CGRect)rect {
  2.  
    [super drawRect:rect];
  3.  
     
  4.  
    CGRect frame = CGRectMake(50, 100, 100, 100);
  5.  
    /*画填充圆
  6.  
    */
  7.  
    CGContextRef context = UIGraphicsGetCurrentContext();
  8.  
    [[UIColor whiteColor] set];
  9.  
    CGContextFillRect(context, rect);
  10.  
     
  11.  
    CGContextAddEllipseInRect(context, frame);
  12.  
    [[UIColor orangeColor] set];
  13.  
    CGContextFillPath(context);
  14.  
     
  15.  
    /*边框圆
  16.  
    */
  17.  
    CGContextSetRGBStrokeColor(context, 255/255.0, 106/255.0, 0/255.0, 1);
  18.  
    CGContextSetLineWidth(context, 5);
  19.  
    CGContextAddArc(context, 50, 70, 20, 0, 2*M_PI, 0);
  20.  
    CGContextDrawPath(context, kCGPathStroke);
  21.  
     
  22.  
    /*画直线
  23.  
    */
  24.  
    CGRect markFrame = CGRectMake(100, 250, 200, 200);
  25.  
    [[UIColor orangeColor] set];
  26.  
    CGContextSetLineWidth(context, 4);
  27.  
    CGPoint points[5];
  28.  
    points[0] = CGPointMake(markFrame.origin.x,markFrame.origin.y);
  29.  
    points[1] = CGPointMake(markFrame.origin.x+markFrame.size.width/4, markFrame.origin.y+markFrame.size.height/2);
  30.  
    points[2] = CGPointMake(markFrame.origin.x+markFrame.size.width/2, markFrame.origin.y+5);
  31.  
    points[3] = CGPointMake(markFrame.origin.x+markFrame.size.width/4*3,markFrame.origin.y+markFrame.size.height/2);
  32.  
    points[4] = CGPointMake(markFrame.origin.x+markFrame.size.width, markFrame.origin.y);
  33.  
     
  34.  
    CGContextAddLines(context, points, 5);
  35.  
    CGContextDrawPath(context, kCGPathStroke);
  36.  
    //边框圆
  37.  
    CGContextSetLineWidth(context, 5);
  38.  
    CGContextAddArc(context, markFrame.origin.x+markFrame.size.width/2, markFrame.origin.y+markFrame.size.height/2, markFrame.size.height/2-2, 0, 2*M_PI, 0);
  39.  
    CGContextDrawPath(context, kCGPathStroke);
  40.  
     
  41.  
    /*曲线
  42.  
    */
  43.  
    [[UIColor redColor] set];
  44.  
     
  45.  
    CGContextSetLineWidth(context, 4.0);
  46.  
    CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
  47.  
    CGContextMoveToPoint(context, 300, 370);
  48.  
    CGContextAddCurveToPoint(context, 193, 320, 100, 370, 100, 370);
  49.  
    CGContextStrokePath(context);
  50.  
    }
  51.  
     

UIViewController:

  1.  
    #import "DrawViewController.h"
  2.  
     
  3.  
    @interface DrawViewController ()
  4.  
     
  5.  
    @end
  6.  
     
  7.  
    @implementation DrawViewController
  8.  
     
  9.  
    - (void)viewDidLoad {
  10.  
    [super viewDidLoad];
  11.  
     
  12.  
    /*
  13.  
    *画虚线圆
  14.  
    */
  15.  
    CAShapeLayer *dotteLine = [CAShapeLayer layer];
  16.  
    CGMutablePathRef dottePath = CGPathCreateMutable();
  17.  
    dotteLine.lineWidth = 2.0f ;
  18.  
    dotteLine.strokeColor = [UIColor orangeColor].CGColor;
  19.  
    dotteLine.fillColor = [UIColor clearColor].CGColor;
  20.  
    CGPathAddEllipseInRect(dottePath, nil, CGRectMake(50.0f, 50.0f, 200.0f, 200.0f));
  21.  
    dotteLine.path = dottePath;
  22.  
    NSArray *arr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
  23.  
    dotteLine.lineDashPhase = 1.0;
  24.  
    dotteLine.lineDashPattern = arr;
  25.  
    CGPathRelease(dottePath);
  26.  
    [self.view.layer addSublayer:dotteLine];
  27.  
     
  28.  
    /*
  29.  
    *画实线圆
  30.  
    */
  31.  
    CAShapeLayer *solidLine = [CAShapeLayer layer];
  32.  
    CGMutablePathRef solidPath = CGPathCreateMutable();
  33.  
    solidLine.lineWidth = 2.0f ;
  34.  
    solidLine.strokeColor = [UIColor orangeColor].CGColor;
  35.  
    solidLine.fillColor = [UIColor clearColor].CGColor;
  36.  
    CGPathAddEllipseInRect(solidPath, nil, CGRectMake(50.0f, 300.0f, 200.0f, 200.0f));
  37.  
    solidLine.path = solidPath;
  38.  
    CGPathRelease(solidPath);
  39.  
    [self.view.layer addSublayer:solidLine];
  40.  
     
  41.  
    /*
  42.  
    *画虚线
  43.  
    */
  44.  
    CAShapeLayer *dotteShapeLayer = [CAShapeLayer layer];
  45.  
    CGMutablePathRef dotteShapePath = CGPathCreateMutable();
  46.  
    [dotteShapeLayer setFillColor:[[UIColor clearColor] CGColor]];
  47.  
    [dotteShapeLayer setStrokeColor:[[UIColor orangeColor] CGColor]];
  48.  
    dotteShapeLayer.lineWidth = 2.0f ;
  49.  
    NSArray *dotteShapeArr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
  50.  
    [dotteShapeLayer setLineDashPattern:dotteShapeArr];
  51.  
    CGPathMoveToPoint(dotteShapePath, NULL, 20,500);
  52.  
    CGPathAddLineToPoint(dotteShapePath, NULL, 20, 285);
  53.  
    CGPathAddLineToPoint(dotteShapePath, NULL, 300,285);
  54.  
    [dotteShapeLayer setPath:dotteShapePath];
  55.  
    CGPathRelease(dotteShapePath);
  56.  
    [self.view.layer addSublayer:dotteShapeLayer];
  57.  
     
  58.  
    /*
  59.  
    *画实线
  60.  
    */
  61.  
    CAShapeLayer *solidShapeLayer = [CAShapeLayer layer];
  62.  
    CGMutablePathRef solidShapePath = CGPathCreateMutable();
  63.  
    [solidShapeLayer setFillColor:[[UIColor clearColor] CGColor]];
  64.  
    [solidShapeLayer setStrokeColor:[[UIColor orangeColor] CGColor]];
  65.  
    solidShapeLayer.lineWidth = 2.0f ;
  66.  
    CGPathMoveToPoint(solidShapePath, NULL, 20, 265);
  67.  
    CGPathAddLineToPoint(solidShapePath, NULL, 300,265);
  68.  
    CGPathAddLineToPoint(solidShapePath, NULL, 300,50);
  69.  
    [solidShapeLayer setPath:solidShapePath];
  70.  
    CGPathRelease(solidShapePath);
  71.  
    [self.view.layer addSublayer:solidShapeLayer];
  72.  
     
  73.  
     
  74.  
    }


 

示图:

原文地址:https://www.cnblogs.com/itlover2013/p/14208144.html