我正在尝试创建一个类,该类在自定义UIView子类中绘制多边形。我让它起作用了,但是现在我想通过把它们四舍五入来平滑边角,我不知道该怎么做。以下是我到目前为止所拥有的:
public class MyView : UIView
{
private List<CGPoint> points;
public MyView(CGRect frame, List<CGPoint> points) : base (frame)
{
this.points = points;
}
public override Draw (CGRect rect)
{
CGContext context = UIGraphics.GetCurrentContext();
context.SetLineWidth(2);
UIColor.Black.SetStroke();
UIColor.Green.SetFill();
CGPath path = new CGPath();
path.AddLines(points.ToArray());
path.CloseSubpath();
context.AddPath(path);
context.Clip();
using (CGColorSpace rgb = CGColorSpace.CreateDeviceRGB())
{
CGGradient gradient = new CGGradient (rgb, new CGColor[]
{
new CGColor(0, 1, 0),
new CGColor(0, 0.5f, 0),
new CGColor(0, 1, 0),
new CGColor(0, 0.5f, 0)
});
context.DrawLinearGradient(gradient,
new CGPoint (path.BoundingBox.Left, path.BoundingBox.Top),
new CGPoint (path.BoundingBox.Right, path.BoundingBox.Bottom),
CGGradientDrawingOptions.DrawsBeforeStartLocation);
}
}
}加上我的分数,我明白了:

我尝试过使用path.AddCurveToPoint和path.AddArc,但我似乎无法让它们按照我想要的方式工作。任何帮助都将不胜感激。
编辑
我和path.AddCurveToPoint玩过,现在看起来是这样的:

一开始是正确的,但后来就完全疯了。不知道我做错了什么。下面是我更新的Draw覆盖:
public override Draw (CGRect rect)
{
CGContext context = UIGraphics.GetCurrentContext();
context.SetLineWidth(2);
UIColor.Black.SetStroke();
UIColor.Green.SetFill();
CGPath path = new CGPath();
path.AddLines(points.ToArray());
path.CloseSubpath();
// updated section
int count = points.Count;
for (int x = 1; x < count; x++)
{
var p = points[x];
if (x != 0)
{
var prev = points[x - 1];
if (prev.Y != p.Y)
{
if (prev.Y > p.Y)
{
path.AddCurveToPoint(prev, new CGPoint(p.X - prev.X, p.Y), p);
}
else
{
//???
}
}
}
}
// end updated section
context.AddPath(path);
context.Clip();
using (CGColorSpace rgb = CGColorSpace.CreateDeviceRGB())
{
CGGradient gradient = new CGGradient (rgb, new CGColor[]
{
new CGColor(0, 1, 0),
new CGColor(0, 0.5f, 0),
new CGColor(0, 1, 0),
new CGColor(0, 0.5f, 0)
});
context.DrawLinearGradient(gradient,
new CGPoint (path.BoundingBox.Left, path.BoundingBox.Top),
new CGPoint (path.BoundingBox.Right, path.BoundingBox.Bottom),
CGGradientDrawingOptions.DrawsBeforeStartLocation);
}
}发布于 2016-06-10 18:41:36
更新:
随着你的更新,我现在看到你的问题了。您正在尝试使用您的点列表作为Bézier控制点和多边形角的绘图点,这将无法工作。
“最简单”的方式,它使用您现有的点列表作为控制点,并在每个控制点之间创建中间节点,并向每个段添加一个四条曲线。
注意:harder的方法是使用点列表作为节点,然后为每个bezier曲线段创建控制点。我将在这里不讨论它,但是许多图表系统使用这个文章作为它们的引用,来自getControlPoints的返回值将用作方法AddCurveToPoint的控制点。
简单的例子:

var color = UIColor.Red;
var color2 = UIColor.White;
var points = new List<CGPoint>() {
new CGPoint(136.49f, 134.6f),
new CGPoint(197.04f, 20.0f),
new CGPoint(257.59f, 20.0f),
new CGPoint(303.0f, 134.6f),
new CGPoint(303.0f, 252.0f),
new CGPoint(28.0f, 252.0f),
new CGPoint(28.0f, 134.6f),
new CGPoint(136.49f, 134.6f)
};
UIBezierPath polygonPath = new UIBezierPath();
polygonPath.MoveTo(points[0]);
polygonPath.AddLineTo(points[1]);
polygonPath.AddLineTo(points[2]);
polygonPath.AddLineTo(points[3]);
polygonPath.AddLineTo(points[4]);
polygonPath.AddLineTo(points[5]);
polygonPath.AddLineTo(points[6]);
polygonPath.ClosePath();
polygonPath.Fill();
color2.SetStroke();
polygonPath.LineWidth = 10.0f;
polygonPath.Stroke();
UIBezierPath smoothedPath = new UIBezierPath();
var m0 = MidPoint(points[0], points[1]);
smoothedPath.MoveTo(m0);
smoothedPath.AddQuadCurveToPoint(m0, points[0]);
var m1 = MidPoint(points[1], points[2]);
smoothedPath.AddQuadCurveToPoint(m1, points[1]);
var m2 = MidPoint(points[2], points[3]);
smoothedPath.AddQuadCurveToPoint(m2, points[2]);
var m3 = MidPoint(points[3], points[4]);
smoothedPath.AddQuadCurveToPoint(m3, points[3]);
var m4 = MidPoint(points[4], points[5]);
smoothedPath.AddQuadCurveToPoint(m4, points[4]);
var m5 = MidPoint(points[5], points[6]);
smoothedPath.AddQuadCurveToPoint(m5, points[5]);
var m6 = MidPoint(points[6], points[0]);
smoothedPath.AddQuadCurveToPoint(m6, points[6]);
smoothedPath.AddQuadCurveToPoint(m0, points[0]);
smoothedPath.ClosePath();
color.SetStroke();
smoothedPath.LineWidth = 5.0f;
smoothedPath.Stroke();原件:
1)我不确定你在看多大的关节平滑度,但CGLineJoin.Round表示LineJoinStyle on a UIBezierPath将创建一个微妙的外观。
2)如果需要重角平滑,可以手动计算每一行的开始/结束,并添加一个ArcWithCenter来连接每个线段。
3)或者没有手工的角弧计算,您可以在使用CGLineJoin.Round时设置一个非常重的线宽,并缩小绘图的大小,以弥补增加的线宽。
示例:
var color = UIColor.FromRGBA(0.129f, 0.467f, 0.874f, 1.000f);
UIBezierPath polygonPath = new UIBezierPath();
polygonPath.MoveTo(new CGPoint(136.49f, 134.6f));
polygonPath.AddLineTo(new CGPoint(197.04f, 20.0f));
polygonPath.AddLineTo(new CGPoint(257.59f, 20.0f));
polygonPath.AddLineTo(new CGPoint(303.0f, 134.6f));
polygonPath.AddLineTo(new CGPoint(303.0f, 252.0f));
polygonPath.AddLineTo(new CGPoint(28.0f, 252.0f));
polygonPath.AddLineTo(new CGPoint(28.0f, 134.6f));
polygonPath.AddLineTo(new CGPoint(136.49f, 134.6f));
polygonPath.ClosePath();
polygonPath.LineJoinStyle = CGLineJoin.Round;
color.SetFill();
polygonPath.Fill();
color.SetStroke();
polygonPath.LineWidth = 30.0f;
polygonPath.Stroke();

:MidPoint方法
protected CGPoint MidPoint(CGPoint first, CGPoint second)
{
var x = (first.X + second.X) / 2;
var y = (first.Y + second.Y) / 2;
return new CGPoint(x: x, y: y);
}https://stackoverflow.com/questions/37753737
复制相似问题