Ios 如何在CGPathGetCurrentPoint添加弧?
我正在绘制甜甜圈图表。我正在使用Ios 如何在CGPathGetCurrentPoint添加弧?,ios,cgpath,cashapelayer,Ios,Cgpath,Cashapelayer,我正在绘制甜甜圈图表。我正在使用CGPath进行绘制,并在绘制后将其添加到CAShapeLayer。首先,我画一个外弧,然后画一条朝向中心的线,现在我想把弧添加到我的当前点上 下面是这个的截图 请看,较小的圆弧应位于直线的末端 我的代码 for (int j = 0; j < numSubject; j++) { int valueForSubject = [datasource valueOfSubjectAtIndex:j andLevel:i inDonut
CGPath
进行绘制,并在绘制后将其添加到CAShapeLayer
。首先,我画一个外弧,然后画一条朝向中心的线,现在我想把弧添加到我的当前点上
下面是这个的截图
请看,较小的圆弧应位于直线的末端
我的代码
for (int j = 0; j < numSubject; j++) {
int valueForSubject = [datasource valueOfSubjectAtIndex:j andLevel:i inDonutChartView:self];
endAngle = [self angleInRadianFromSubjectValue:valueForSubject];
DLog(@"Angle - %f",RADIANS_TO_DEGREES(endAngle))
//path
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, prevPoint.x, prevPoint.y, radius, startAngle, endAngle, NO);
CGPoint currentPoint = CGPathGetCurrentPoint(path);
//find angles from start point to center and end point to center
CGFloat angle1 = [self angleFromFirstPoint:prevPoint secondPoint:centerPoint];
CGFloat angle2 = [self angleFromFirstPoint:CGPathGetCurrentPoint(path) secondPoint:centerPoint];
double endX1 = cos(angle1) * LEVEL_WIDTH + prevPoint.x;
double endY1 = sin(angle1) * LEVEL_WIDTH + prevPoint.y;
double endX2 = cos(angle2) * LEVEL_WIDTH + CGPathGetCurrentPoint(path).x;
double endY2 = sin(angle2) * LEVEL_WIDTH + CGPathGetCurrentPoint(path).y;
//first connect current point to end2 point then draw arc and then connect to end1
CGPathAddLineToPoint(path, NULL, endX2, endY2);
//CGPathAddArcToPoint(path, NULL, CGPathGetCurrentPoint(path).x, CGPathGetCurrentPoint(path).y, endX2, endY2, radius - LEVEL_WIDTH);
CGPathAddArc(path, NULL, CGPathGetCurrentPoint(path).x , CGPathGetCurrentPoint(path).y, radius - LEVEL_WIDTH, endAngle, startAngle, YES);
//CGPathAddLineToPoint(path, NULL, endX1, endY1);
//CGPathCloseSubpath(path);
A3DonutChartShapeLayer* arcLayer = [[A3DonutChartShapeLayer alloc]init];
arcLayer.path = path;
//arcLayer.frame = CGPathGetBoundingBox(path);
arcLayer.lineWidth = BORDER_WIDTH;
arcLayer.strokeColor = [UIColor blackColor].CGColor;
arcLayer.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:arcLayer];
for(int j=0;j
有任何指针吗?选项1:CorePlot API 我不想自己画画,也不考虑所有的场景,我更愿意使用: 选项2:Google图表API
NSString* myurl=@"http://chart.apis.google.com/chart?cht=pc&chd=t:120,45%7c120,60,50,70,60&chs=300x200&chl=%7c%7chelo%7cwrd%7cindia%7cpak%7cban&chco=FFFFFF%7cFFFFFF,e72a28%7ca9d331%7cffce08%7c8a2585%7c184a7d";
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:myurl] cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLResponse* response;
NSError* error;
NSData *imageData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
UIImage *myimage = [[UIImage alloc] initWithData:imageData];
选项3:Core Graphics and ARC drawing的精美教程
选项4:使用HTML+CSS+JavaScript
添加UIWebView并显示本地HTML页面,该页面将根据传递给JavaScript的值生成动态图
NSString* myurl=@"http://chart.apis.google.com/chart?cht=pc&chd=t:120,45%7c120,60,50,70,60&chs=300x200&chl=%7c%7chelo%7cwrd%7cindia%7cpak%7cban&chco=FFFFFF%7cFFFFFF,e72a28%7ca9d331%7cffce08%7c8a2585%7c184a7d";
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:myurl] cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLResponse* response;
NSError* error;
NSData *imageData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
UIImage *myimage = [[UIImage alloc] initWithData:imageData];
选项3:Core Graphics and ARC drawing的精美教程
选项4:使用HTML+CSS+JavaScript
添加UIWebView并显示本地HTML页面,该页面将根据传递给JavaScript的值生成动态图
- 这是:
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100, startAngle, endAngle, NO);
CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100-50, endAngle, startAngle, YES);
CGPathCloseSubpath(path);
这是:
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100, startAngle, endAngle, NO);
CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100-50, endAngle, startAngle, YES);
CGPathCloseSubpath(path);
关于你实际问题的说明
路径已经这样做了。直线、圆弧、曲线等都是从当前点添加的。看起来您正在为CGPathAddArc(…)
中的x和y参数传递当前点,但这些参数用于圆弧的中心,而不是起点
做油炸圈饼图表的更好方法。 这就是说,有一种更好的制作甜甜圈图表的方法。首先在甜甜圈的中心画一条弧,然后通过划动该弧创建一条新的路径。这使您可以非常轻松地自定义甜甜圈的宽度。我已经对此进行了描述,并在中对代码进行了详细的分解,但这里是形状本身的一个较短版本 执行此操作的代码 在甜甜圈的中心创建圆弧(下图中的橙色线) 通过笔划该路径创建新路径(上图中的点形状和下图中的填充形状): 关于你实际问题的说明 路径已经这样做了。直线、圆弧、曲线等都是从当前点添加的。看起来您正在为
CGPathAddArc(…)
中的x和y参数传递当前点,但这些参数用于圆弧的中心,而不是起点
做油炸圈饼图表的更好方法。 这就是说,有一种更好的制作甜甜圈图表的方法。首先在甜甜圈的中心画一条弧,然后通过划动该弧创建一条新的路径。这使您可以非常轻松地自定义甜甜圈的宽度。我已经对此进行了描述,并在中对代码进行了详细的分解,但这里是形状本身的一个较短版本 执行此操作的代码 在甜甜圈的中心创建圆弧(下图中的橙色线) 通过笔划该路径创建新路径(上图中的点形状和下图中的填充形状):
增加了几个选项。但要求您不能使用任何第三方库…这就是为什么我使用cgpath和cashapelayerForth选项不使用任何第三方库。它完全是特定于开发人员的代码。第三个选项是一个非常好的教程,可以使用核心图形绘制圆弧和颜色方案。您的第三个答案是:已经知道,第四个答案:我必须使用cashapelayer和cgpath。这就是为什么我要问一个疑问,否则我也知道许多绘制图表的方法…增加了一些选项。但要求是你不能使用任何第三个p