在 Cocoa 中绘制一堆(~200)彩色矩形的最佳方法是什么?

发布于 2024-07-17 11:16:30 字数 105 浏览 4 评论 0原文

我当前的计划是通过子类化 NSView 来绘制矩形,但这对于我正在尝试做的事情来说似乎是一种非常低效的方法,即绘制一堆固定的、不重叠的矩形,这些矩形偶尔会改变颜色。 有没有更好的办法? 谢谢。

My current plan is to draw the rectangles by subclassing NSView, but that seems like a very inefficient way for what I'm trying to do, which is to draw a bunch of fixed, non-overlapping rectangles that changes colors once in a while. Is there a better way? Thanks.

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

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

发布评论

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

评论(3

少跟Wǒ拽 2024-07-24 11:16:30

您可以尝试使用 CALayer,如下所示:http://theocacao .com/document.page/555

You can try using CALayers, kind of like this: http://theocacao.com/document.page/555.

陈甜 2024-07-24 11:16:30

如果它们都是相同的颜色或图像,您可能会发现单个 CGLayer 效率更高。 该 API 的目的是多次绘制相同的内容。

另一方面,如果矩形独立移动或具有不同的颜色或图像,则 Core Animation 绝对是最佳选择。

If they're all the same color or image, you may find a single CGLayer more efficient. The purpose of that API is drawing the same thing many times.

On the other hand, if the rectangles move independently or have different colors or images on them, Core Animation is definitely the way to go.

春庭雪 2024-07-24 11:16:30

核心动画对于游戏来说是一项很棒的技术,但如果您想暂时坚持使用 NSView,您可以创建一个类似于 NSCell 的类,游戏板视图使用它来实现定位和绘图。 这与许多 Cocoa 控件类的工作方式类似,它们使用单​​个单元格(具有不同的值)在视图内绘制多个项目。

请记住,使用单独的 NSView 对象可能已经足够快了,但无论速度有何差异,此策略都允许您以有意义的方式分离逻辑。

Core Animation would be a great technology for a game, but if you want to stick with NSView for the time being you could create a class similar to NSCell that the gameboard view uses to implement positioning and drawing. This would work in a similar way as many Cocoa control classes, which use a single cell (with different values) to draw multiple items inside a view.

Keep in mind that using individual NSView objects may very well be more than fast enough, but regardless of any speed differences this strategy allows you to separate the logic in a way that makes sense.

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