不使用drawrect方法获取UIView的Context

发布于 2024-11-18 07:15:00 字数 107 浏览 1 评论 0原文

我需要在不使用 drawrect 方法的情况下在 uiview 上绘图,但我不知道如何在任何时间点动态获取图形上下文?

例如:当用户从工具栏中的弹出窗口中选择添加行时,我需要添加一行。

I need to draw on my uiview without using the drawrect method but i don't know how to get the graphics context dynamically at any point of time?

For example: I need to add a line when user selected the add line from from pop over in my toolbar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云醉月微眠 2024-11-25 07:15:00

这是不可能的。您必须使用某种标记来指示新行,然后调用 setNeedsDisplay 方法,以便它调用 drawRect: 方法。您可以在 drawRect: 方法中检查标记并相应地绘制线条。

您必须保存状态。可能添加了一组行,以便您可以在调用 drawRect: 方法时使用它来绘制视图。

替代方案

您可以使用图像视图来实现此目的。您可以这样做,

UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
/* Draw line here */
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这样您就不会拥有状态,而只需将线条添加到图像中即可。

This can't be done. You will have to have some kind of marker to indicate a new line and then call setNeedsDisplay method so that it invokes the drawRect: method. You can check for the marker in the drawRect: method and draw the line accordingly.

You will have to save state. Probably an array of lines added so that you can use it to draw the view when drawRect: method is invoked.

An alternative

You can use an image view for this purpose. You can do this,

UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
/* Draw line here */
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This way you won't have state but you simply add the line to the image.

海之角 2024-11-25 07:15:00

您的视图在 drawRect: 之外没有任何图形上下文。您不能在 drawRect: 之外进行任意绘图调用并让它们持续存在。

如果您需要动态地将线条等添加到视图中,则必须首先将线条存储在数组或其他结构中,然后在 drawRect: 内迭代该结构并绘制每个项目。

或者,使每行本身成为自定义视图(或图层)并将其添加为子视图。

Your view has no graphics context outside of drawRect:. You cannot make arbitrary drawing calls outside of drawRect: and have them persist.

If you need to dynamically add lines etc. to a view, you must first store the lines in an array or other structure, and then iterate that structure inside drawRect: and draw each item.

Alternatively, make each line a custom view itself (or a layer) and add it as a subview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文