Xamarin iOS CGPath绘制带弯曲/圆角的多边形

时间:2016-06-10 17:08:09

标签: c# ios xamarin polygon cgpath

我正在尝试创建一个在自定义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);
        }
    }
}

有了我投入的分数,我得到了这个:

jQuery docs

我尝试过使用path.AddCurveToPointpath.AddArc,但我似乎无法让它们以我想要的方式工作。任何帮助将不胜感激。

修改

我和path.AddCurveToPoint一起玩,现在看起来像这样:

enter image description here

它正确启动,但后来完全疯了。不知道我在这里做错了什么。这是我更新的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);
    }
}

1 个答案:

答案 0 :(得分:2)

更新

随着您的更新,我现在看到您的问题。您正在尝试将您的点列表用作Bézier控制点和多边形角点的绘制点,这些都不起作用。

使用现有点列表作为控制点并在每个控制点之间创建中间节点并为每个段添加四边形曲线的“最简单”方式。

注意:更难方式是使用您的点列表作为节点,并为每个贝塞尔曲线段创建控制点。我不会在此处讨论,但许多图表系统使用此article作为参考,getControlPoints的返回值将用作方法AddCurveToPoint的控制点。

简单的例子:

enter image description here

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.RoundLineJoinStyle的{​​{1}}会产生微妙的外观。

2)如果需要较大的角平滑,可以手动计算每条线的开始/结束,并添加一个ArcWithCenter以连接每个线段。

3)或者,如果没有手动拐角弧计算,您可以在使用UIBezierPath时设置非常粗的线宽,并将图形放大一些,以减小线宽。

示例:

CGLineJoin.Round

enter image description here

:MidPoint方法

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();