ios - CGPath AddLineToPoint 不工作

标签 ios xamarin.ios

我正在尝试使用 UIView 中的图形上下文在我的 Xamarin.iOS 应用程序中绘制一条对角线:

public override void Draw(CGRect rect)
{
    base.Draw(rect);

    using (var gctx = UIGraphics.GetCurrentContext())
    {
        var path = new CGPath();
        path.AddLineToPoint(rect.Width, rect.Size.Height);
        gctx.AddPath(path);
        gctx.SetLineWidth(5);
        gctx.SetFillColor(UIColor.Red.CGColor);
        gctx.FillPath();
        gctx.DrawPath(CGPathDrawingMode.FillStroke);
    }
}

我希望看到一条红色对角线,但我什么也没看到?

最佳答案

解决方案:

您可以通过如下代码片段来实现这一点:

    public override void Draw(CGRect rect)
    {
        base.Draw(rect);

        using (var gctx = UIGraphics.GetCurrentContext())
        {
            gctx.SetStrokeColor(UIColor.Red.CGColor);
            gctx.SetLineWidth(2.0f);
            gctx.MoveTo(0, 0);
            gctx.AddLineToPoint(rect.Width, rect.Height);
            gctx.StrokePath();
        }

    }

它的工作原理如下:

enter image description here

关于ios - CGPath AddLineToPoint 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47423866/

相关文章:

ios - 如何在 UIImageView 中显示 UIImage?

ios - 如何使用 Typhoon 将属性注入(inject)基类

ios - 调用 "didSelectItemAt"时从单元格内调用函数

ios - 如何访问连续的两个TextField

c# - 如何将分隔符添加到 UITabbar ios

ios - Monotouch 中的 DequeueReusableCell 导致单元格在滚动时重新排列

ios - 为具有不同字体大小的标签设置顶部对齐方式,Swift 3

ios - Xamarin.iOS 属性与 { get; 的绑定(bind)问题} vs { 得到;放; }

iphone - 为什么仪器不执行我的 Monotouch 应用程序?

objective-c - 与 iOS 存储和同步图像