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?
相关问题
- NSOutlineView drag line stuck + blue border
- How to plot line in matlab with theta/rho data
- iphone sdk see size of local file (one created
- How can you detect the connection and disconnectio
- QuickLook Plugin Failing with sandboxing error
相关文章
- Converting (u)int64_t to NSNumbers
- “getter” keyword in @property declaration in Objec
- NSMenuItem KeyEquivalent “ ”(space) bug
- Difference between SuspendLayout and BeginUpdate
- Detect if cursor is hidden on Mac OS X
- NSNumberFormatter doesn't allow typing decimal
- Is subclassing NSNotification the right route if I
- Adb install progress bar
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.
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
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.
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.