与 UIView 边界相关的 drawRect 的 aRect 参数

发布于 2024-11-04 20:09:30 字数 816 浏览 0 评论 0 原文

我正在努力创建我的第一个自定义 UIView 并学习 drawRect 方法的语义。

我遇到过建议(对我来说有意义的建议)仅在CGRect<的范围内绘制/code> 作为参数传入,而不是总是在 UIViewbounds 内绘制所有内容:

- (void)drawRect:(CGRect)aRect {
    // draw, draw, draw...
}

我现在想要找出的是是否有关于什么的任何限制bounds 和传入的 aRect 之间存在关系。特别是,我想知道 aRect 是否保证完全位于 < code>bounds,或者是否可以延伸到bounds之外。

如果我的 UIView 之外的某些代码传递,例如,将一个非常大的 CGRect 传递到 setNeedsDisplayInRect:(NSRect)invalidRect 中,那么底层代码是否会盲目地将相同的 CGRect 传递到我的drawRect 方法,或者它是否进行某种交集并始终传入一个合理的矩形?

我在任何文档中都没有找到这个问题的答案。这是我根本不应该担心的事情吗?或者我应该总是自己将 boundsaRect 相交?

I'm working on creating my first custom UIView and learning about the semantics of the drawRect method.

I have encountered advice (advice that makes sense to me) to only draw within the scope of the CGRect that is passed in as an argument, rather than always drawing everything within the UIView's bounds:

- (void)drawRect:(CGRect)aRect {
    // draw, draw, draw...
}

What I'm trying to find out now is whether there are any constraints about what relationship there is between bounds and the aRect that gets passed in. In particular, I'm wondering if aRect is guaranteed to be entirely inside bounds, or whether it may extends outside of bounds.

If some code outside of my UIView passes, for example, a very large CGRect into setNeedsDisplayInRect:(NSRect)invalidRect, will the underlying code just blindly pass the same CGRect into my drawRect method, or does it do some sort of intersection and always pass in a sensible rectangle?

I haven't found an answer to this in any documentation. Is this something I shouldn't even worry about? Or should I always intersect bounds and aRect on my own?

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

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

发布评论

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

评论(1

記柔刀 2024-11-11 20:09:30

来自查看编程指南

在调用视图的drawRect:方法之前,UIKit会为视图配置基本的绘图环境。具体来说,它创建一个图形上下文并调整坐标系和剪切区域以匹配坐标系和视图的可见边界。

您不会被要求在您的视野之外进行绘制。即使是这样,也应该设置剪辑矩形,以使绘图不会产生任何效果。

From the View Programming Guide:

Before calling your view’s drawRect: method, UIKit configures the basic drawing environment for your view. Specifically, it creates a graphics context and adjusts the coordinate system and clipping region to match the coordinate system and visible bounds of your view.

You won't be asked to draw outside your view. Even if you were, the clip rect should be set such that the drawing would have no effect.

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