在drawRect中部分绘制UIView的最有效方法:

发布于 2024-09-28 11:11:42 字数 229 浏览 1 评论 0原文

我在 UIView 子类中尽可能多地使用 setNeedsDisplayInRect:,但我不知道在 drawRect: 中做什么。检测 UIView 的哪些部分必须绘制的最佳方法是什么?现在我已经将 UIView 分成了几个 CGRect 。对于每个 CGRect,我都会调用 CGRectContainsRect() 来检查是否需要绘制该 CGRect。这是正确的方法,还是有更好的方法?

I'm using setNeedsDisplayInRect: as much as possible in my UIView subclass, but I don't know what to do in drawRect:. What is the best way to detect which parts of the UIView have to be drawn? Right now I've divided my UIView into several CGRect's. For each CGRect I'm calling CGRectContainsRect() to check whether that CGRect needs to be drawn or not. Is that the correct way, or is there a better way?

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

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

发布评论

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

评论(2

绮筵 2024-10-05 11:11:43

您将需要绘制任何位于无效矩形中的矩形,因此您将需要使用 CGRectIntersectsRect() 。话虽如此,我见过少数情况下部分绘制 UIView 是有意义的 - 除非您在滚动视图内有一个非常大的单个视图,否则您也可以绘制整个视图。

You'll want to draw any rect that is even partially in the invalidated rect, so you'll want to use CGRectIntersectsRect(). Having said that, I've seen few cases where it makes sense to partially draw UIViews -- unless you have a very large single view inside of a scroll view, you may as well draw the whole thing.

软糖 2024-10-05 11:11:43

您正在查看一个经典的空间分区任务。根据您的视图复杂性,您可以使用不同的策略来找出需要绘制的内容。如果您的场景非常简单,那么绘制所有内容或将场景划分为离散区域并进行边界矩形检查就可以了。对于非常复杂的场景,有几种很好的数据结构(例如八叉树)可以将场景表示为树,并对给定区域内的对象执行空间查询。

查看 Wikipedia 上的空间分区主题:http://en.wikipedia.org/wiki/Space_partitioning

You're looking at a classic spatial partitioning task. Depending on your view complexity, you can use different strategies to find out what needs to be drawn. If your scene is very simple, then drawing everything or partitioning the scene into discrete areas and doing your bounding rectangle check is fine. For very complex scenes, there are several good data structures (such as octrees) for representing your scene as a tree, and performing spacial queries for objects within a given region.

Check out the topic of Spacial Partitioning on Wikipedia: http://en.wikipedia.org/wiki/Space_partitioning

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