我试图在我的Xamarin.iOS应用程序中使用UIView中的图形上下文绘制一条对角线。
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);
}
}我想看到一条红色的对角线,但我什么也没看到?
发布于 2017-11-22 06:25:43
解决方案:
您可以通过下面的代码片段来实现这一点:
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();
}
}它的工作方式如下:

https://stackoverflow.com/questions/47423866
复制相似问题