我应该如何在 ViewController 的默认 View 上绘图?

发布于 2024-12-27 09:56:54 字数 306 浏览 2 评论 0原文

XCode 4.2(我使用过的唯一 XCode 版本)创建了一个具有默认 View 属性的 ViewController。

我看不到这个默认视图的代码,所以我无法在它上面绘图(因为我无法访问drawRect)。这是正确的吗?

假设上述答案是我不会画画。如果我想在窗口上绘制,我应该:

  1. 向默认视图添加一个子视图,然后在子视图上绘制(通过drawRect)?或者
  2. 将默认视图替换为我创建的视图,因此可以通过drawRect进行绘制?

这不是如何做到这一点的问题,我想知道最好的方法是什么。谢谢。

XCode 4.2 (the only version of XCode I've ever used) creates a ViewController with a default View property.

I cannot see the code for this default View so I cannot draw on it (because I cannot access drawRect). Is that correct?

Assuming answer to the above is that I cannot draw. If I want to draw on the window should I:

  1. Add a subview to the default View and then draw (via drawRect) on the subview? or
  2. Replace the default View with one that I create and can therefore draw on via drawRect?

This is not a question of how to do this, I want to know what would be the best way. Thanks.

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

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

发布评论

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

评论(1

三寸金莲 2025-01-03 09:56:54

如果您需要在视图中进行自定义绘图,那么您应该定义自己的自定义 UIView 类,在其中重写 drawRect 来进行自定义绘图。

现在,您是遵循方法 1. 还是方法 2. 取决于您的应用程序的用途。两者都是完全合理的。

例如,假设您想要进行自定义绘图,但也想显示某种控件(例如,清除绘图、重置绘图等),那么我认为如果您有的话,这可能是一个更好的设计您将自定义视图和其他视图添加到作为子视图的根视图。但这只是一个例子,这取决于您想要实现的目标。

希望这会有所帮助...

If you need to do custom drawing in you view, then you should define your own custom UIView class, where you override drawRect to do the custom drawing.

Now, wether you follow either approach 1. or 2. depends on what your app does. Both are perfectly reasonable.

Say, for example, that you would like to do your custom drawing, but also show some kind of controls (e.g. to clear the drawing, to reset it, and so on), then I think it could be a better design if you have a root view that you add both the custom view and the other view to as subviews. But this is just an example and it depends on what you are trying to accomplish.

Hope this helps a bit...

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