使用UIBezierPath绘制图形曲线 [英] Draw Graph curves with UIBezierPath
问题描述
我正在申请中绘制图表。我的问题是我想画线连接顶点作为曲线。目前我用 UIBezierPath
的函数 addLineToPoint:
绘制它们。我想把它们画成曲线。我很清楚 UIBezierPath
有以下两个函数来支持这个功能。
I'm drawing a graph in my application. My problem is that I want to draw line joining vertex points as curves. Currently I'm drawing them with UIBezierPath
's function addLineToPoint:
. I want to draw them as curves. I'm well aware that UIBezierPath
has following two functions to support this feature.
立方曲线: addCurveToPoint:controlPoint1:controlPoint2:
二次曲线: addQuadCurveToPoint:控制点:
但问题是我没有控制点。我只有两个终点。我都没有找到确定控制点的方法/公式。有人能帮我一下吗?如果有人可以提出一些替代方案,我将不胜感激......
But problem is that I don't have control points. All i have is two end points. Neither I found a method/formula to determine control points. Can anyone help me here? I will appreciate if someone can suggest some alternative...
推荐答案
所以我根据@Fogmeister的答案找到了解决办法。
SO I found a work around based on @Fogmeister's answer.
UIBezierPath *path = [UIBezierPath bezierPath];
[path setLineWidth:3.0];
[path setLineCapStyle:kCGLineCapRound];
[path setLineJoinStyle:kCGLineJoinRound];
// actualPoints are my points array stored as NSValue
NSValue *value = [actualPoints objectAtIndex:0];
CGPoint p1 = [value CGPointValue];
[path moveToPoint:p1];
for (int k=1; k<[actualPoints count];k++) {
NSValue *value = [actualPoints objectAtIndex:k];
CGPoint p2 = [value CGPointValue];
CGPoint centerPoint = CGPointMake((p1.x+p2.x)/2, (p1.y+p2.y)/2);
// See if your curve is decreasing or increasing
// You can optimize it further by finding point on normal of line passing through midpoint
if (p1.y<p2.y) {
centerPoint = CGPointMake(centerPoint.x, centerPoint.y+(abs(p2.y-centerPoint.y)));
}else if(p1.y>p2.y){
centerPoint = CGPointMake(centerPoint.x, centerPoint.y-(abs(p2.y-centerPoint.y)));
}
[path addQuadCurveToPoint:p2 controlPoint:centerPoint];
p1 = p2;
}
[path stroke];
这篇关于使用UIBezierPath绘制图形曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!