ios 画线平滑_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];

}

示图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值