在 Cocoa 中绘制一堆(~200)彩色矩形的最佳方法是什么?
我当前的计划是通过子类化 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用
CALayer
,如下所示:http://theocacao .com/document.page/555。You can try using
CALayer
s, kind of like this: http://theocacao.com/document.page/555.如果它们都是相同的颜色或图像,您可能会发现单个 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.
核心动画对于游戏来说是一项很棒的技术,但如果您想暂时坚持使用 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.