iOS画圆、画线

UIView:

 

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGRect frame = CGRectMake(50, 100, 100, 100);
    /*画填充圆
     */
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
    CGContextFillRect(context, rect);
    
    CGContextAddEllipseInRect(context, frame);
    [[UIColor orangeColor] set];
    CGContextFillPath(context);
    
    /*边框圆
     */
    CGContextSetRGBStrokeColor(context, 255/255.0, 106/255.0, 0/255.0, 1);
    CGContextSetLineWidth(context, 5);
    CGContextAddArc(context, 50, 70, 20, 0, 2*M_PI, 0);
    CGContextDrawPath(context, kCGPathStroke);

    /*画直线
     */
    CGRect markFrame = CGRectMake(100, 250, 200, 200);
    [[UIColor orangeColor] set];
    CGContextSetLineWidth(context, 4);
    CGPoint points[5];
    points[0] = CGPointMake(markFrame.origin.x,markFrame.origin.y);
    points[1] = CGPointMake(markFrame.origin.x+markFrame.size.width/4, markFrame.origin.y+markFrame.size.height/2);
    points[2] = CGPointMake(markFrame.origin.x+markFrame.size.width/2, markFrame.origin.y+5);
    points[3] = CGPointMake(markFrame.origin.x+markFrame.size.width/4*3,markFrame.origin.y+markFrame.size.height/2);
    points[4] = CGPointMake(markFrame.origin.x+markFrame.size.width, markFrame.origin.y);
    
    CGContextAddLines(context, points, 5);
    CGContextDrawPath(context, kCGPathStroke);
    //边框圆
    CGContextSetLineWidth(context, 5);
    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);
    CGContextDrawPath(context, kCGPathStroke);
    
    /*曲线
     */
    [[UIColor redColor] set];
    
    CGContextSetLineWidth(context, 4.0);
    CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
    CGContextMoveToPoint(context, 300, 370);
    CGContextAddCurveToPoint(context,  193, 320, 100, 370, 100, 370);
    CGContextStrokePath(context);
}


 

UIViewController:

 

#import "DrawViewController.h"

@interface DrawViewController ()

@end

@implementation DrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    /*
     *画虚线圆
     */
    CAShapeLayer *dotteLine =  [CAShapeLayer layer];
    CGMutablePathRef dottePath =  CGPathCreateMutable();
    dotteLine.lineWidth = 2.0f ;
    dotteLine.strokeColor = [UIColor orangeColor].CGColor;
    dotteLine.fillColor = [UIColor clearColor].CGColor;
    CGPathAddEllipseInRect(dottePath, nil, CGRectMake(50.0f,  50.0f, 200.0f, 200.0f));
    dotteLine.path = dottePath;
    NSArray *arr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
    dotteLine.lineDashPhase = 1.0;
    dotteLine.lineDashPattern = arr;
    CGPathRelease(dottePath);
    [self.view.layer addSublayer:dotteLine];
    
    /*
     *画实线圆
     */
    CAShapeLayer *solidLine =  [CAShapeLayer layer];
    CGMutablePathRef solidPath =  CGPathCreateMutable();
    solidLine.lineWidth = 2.0f ;
    solidLine.strokeColor = [UIColor orangeColor].CGColor;
    solidLine.fillColor = [UIColor clearColor].CGColor;
    CGPathAddEllipseInRect(solidPath, nil, CGRectMake(50.0f,  300.0f, 200.0f, 200.0f));
    solidLine.path = solidPath;
    CGPathRelease(solidPath);
    [self.view.layer addSublayer:solidLine];
    
    /*
     *画虚线
     */
    CAShapeLayer *dotteShapeLayer = [CAShapeLayer layer];
    CGMutablePathRef dotteShapePath =  CGPathCreateMutable();
    [dotteShapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    [dotteShapeLayer setStrokeColor:[[UIColor orangeColor] CGColor]];
    dotteShapeLayer.lineWidth = 2.0f ;
    NSArray *dotteShapeArr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
    [dotteShapeLayer setLineDashPattern:dotteShapeArr];
    CGPathMoveToPoint(dotteShapePath, NULL, 20,500);
    CGPathAddLineToPoint(dotteShapePath, NULL, 20, 285);
    CGPathAddLineToPoint(dotteShapePath, NULL, 300,285);
    [dotteShapeLayer setPath:dotteShapePath];
    CGPathRelease(dotteShapePath);
    [self.view.layer addSublayer:dotteShapeLayer];
    
    /*
     *画实线
     */
    CAShapeLayer *solidShapeLayer = [CAShapeLayer layer];
    CGMutablePathRef solidShapePath =  CGPathCreateMutable();
    [solidShapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    [solidShapeLayer setStrokeColor:[[UIColor orangeColor] CGColor]];
    solidShapeLayer.lineWidth = 2.0f ;
    CGPathMoveToPoint(solidShapePath, NULL, 20, 265);
    CGPathAddLineToPoint(solidShapePath, NULL, 300,265);
    CGPathAddLineToPoint(solidShapePath, NULL, 300,50);
    [solidShapeLayer setPath:solidShapePath];
    CGPathRelease(solidShapePath);
    [self.view.layer addSublayer:solidShapeLayer];
    
    
}


 

 

示图:

技术分享

技术分享

技术分享

 

 
文章来自:http://www.cnblogs.com/wxios/p/4533106.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3