在 NSView-drawRect: 方法中绘制线条的首选/推荐方法是什么?
我在 Cocoa 的 NSView 级别找不到任何线条绘制原语。我唯一找到的是NSBezierPath
。这是首选方式吗?还是还有其他我没发现的方法?
I couldn't found any line drawing primitive in Cocoa at NSView level. The only thing I've been found is NSBezierPath
. Is this a preferred way? Or is there another way which I couldn't discovered?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSBezierPath
正是您应该使用的。如果您只想从一个点到另一个点绘制一条直线,请使用类方法:+StrokeLineFromPoint:(NSPoint)point1 toPoint:(NSPoint)point2
NSBezierPath
is exactly what you should be using. If you just want to draw a straight line from one point to another, use the class method:+strokeLineFromPoint:(NSPoint)point1 toPoint:(NSPoint)point2
Cocoa 使用隐式绘图堆栈和失效模型。在你的 NSView 中,当状态改变导致视图以不同的方式绘制时,你调用 -[self setNeedsDisplay:] 来告诉绘图系统你需要重新绘制。在不久的将来的某个时刻,实际上是当前事件循环的结束,您的视图的drawRect:方法将被调用。这是你画任何你想画的东西的机会。
有一个隐式焦点堆栈,这意味着当调用视图的drawRect:时,绘图将集中在其所在窗口中的视图边界上并剪切到视图的边界。然后,您可以调用诸如 [[NSColor redColor] set]; 之类的函数和 NSRectFill([自身边界]);
下面是一个示例:
视图应绘制一条对角线,每次单击该对角线时,该线都应改变颜色。
Cocoa uses an implicit drawing stack, and an invalidation model. In your NSView, when state changes that would cause the view to draw differently, you invoke -[self setNeedsDisplay:] to tell the drawing system that you need to be redrawn. At some point in very near future, actually the end of the current event loop, your view's drawRect: method will be called. That's your opportunity to draw anything you'd like.
There's an implicit focus stack, meaning that when your view's drawRect: is called, drawing is focused on and clipped to the bounds of your view in the window it is in. You can then call functions like [[NSColor redColor] set]; and NSRectFill([self bounds]);
Here's an example:
The view should draw a diagonal line, and each time it is clicked the line should change color.
我尝试了 Jon 给出的示例,发现我需要对上面的代码示例添加 2 个小修复。
一旦我解决了这个问题,我发现代码片段非常有用。
注意:您可能还需要取消分配 NSColor。
I tried the example given by Jon and found that i needed to add 2 minor fixes to the code sample above.
Once i fixed this, i found the code snippit very useful.
NOTE: you probably need to dealloc the NSColor as well.
只是为了添加一些信息,我养成了确保在绘制之前和之后保存和恢复图形状态的习惯,以保持工作顺利。
Just to add some info, I make a habit of making sure the graphics state is saved and restored before and after drawing, to keep things zippy.