我有一个UIViewController包含2个子视图(从UIView派生的类),使用UIGraphics.GetCurrentContext进行自定义绘图.绘图在应用程序启动时工作正常.我在视图控制器的ViewDidLoad()中连接子视图,如下所示:
DigitalNumberView dnv = new DigitalNumberView();
dnv.Frame = new System.Drawing.RectangleF(10, 10, 750, 190);
View.AddSubview(dnv);
CircleView tuner = new CircleView(dnv);
tuner.Frame = new System.Drawing.RectangleF(570, 220, 190, 190);
View.AddSubview(tuner);
Run Code Online (Sandbox Code Playgroud)
我需要第一个子视图来绘制自定义内容,具体取决于第二个子视图的功能.当第二个子视图获得TouchesEnded时,我在第一个子视图中调用一个显示新数据的函数.第一个子视图中的绘图使用UIGraphics.GetCurrentContext,它在此特定工作流中变为null.以下是它如何绘制的示例:
SetNeedsDisplay();
CGContext context = UIGraphics.GetCurrentContext ();
if (null == context) {
Console.WriteLine("Retrieved null CGContext");
return;
}
RectangleF r = new RectangleF(25, 10, 180, 180);
ClearNumber(context, r);
DisplayNumber(context, f, r);
UIGraphics.EndImageContext();
Run Code Online (Sandbox Code Playgroud)
为什么UIGraphics.GetCurrentContext仅在此工作流程中返回null,但在启动时不返回?还有另一种方法可以达到同样的效果吗?我只是错过了一些东西吗?