iOS画圆、画线
https://blog.csdn.net/csdndimo/article/details/84842727
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];
-
-
-
}
示图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2015-12-29 Mac上的抓包工具Charles