You basically override drawRect: and implement your drawing, everything else should be automatic... Below is a snippet of code from a view which draws a circle where the user clicks.
Ali
The interface:
@interface MyView : NSView {
NSPoint center;
float radius;
}
And part of the code:
@implementation MyView
- (id)initWithFrame

NSRect)frame {
[super initWithFrame:frame];
center.x = 50.0;
center.y = 50.0;
radius = 10.0;
return self;
}
// Recommended way to handle events is to override NSResponder (superclass
// of NSView) methods in the NSView subclass. One such method is mouseUp:.
// These methods get the event as the argument. The event has the mouse
// location in window coordinates; use convertPoint:fromView: (with "nil"
// as the view argument) to convert this point to local view coordinates.
//
// Note that once we get the new center, we call setNeedsDisplay:YES to
// mark that the view needs to be redisplayed (which is done automatically
// by the AppKit).
- (void)mouseUp

NSEvent *)event {
NSPoint eventLocation = [event locationInWindow];
center = [self convertPoint:eventLocation fromView:nil];
[self setNeedsDisplay:YES];
}
// setRadiusFromControl: is an action method which lets you change the radius of the dot.
// We assume the sender is a control capable of returning a floating point
// number; so we ask for it's value, and mark the view as needing to be
// redisplayed. A possible optimization is to check to see if the old and
// new value is the same, and not do anything if so.
- (void)setRadiusFromControl

id)sender {
radius = [sender floatValue];
[self setNeedsDisplay:YES];
}
// drawRect: should be overridden in subclassers of NSView to do necessary
// drawing in order to recreate the the look of the view. It will be called
// to draw the whole view or parts of it (pay attention the rect argument);
// it will also be called during printing if your app is set up to print.
// Here we first clear the view to white, then draw a red dot at its
// current location.
- (void)drawRect

NSRect)rect {
NSRect dotRect;
[[NSColor whiteColor] set];
NSRectFill([self bounds]); // Same as [[NSBezierPath bezierPathWithRect:[self bounds]] fill]
dotRect.origin.x = center.x - radius;
dotRect.origin.y = center.y - radius;
dotRect.size.width = 2 * radius;
dotRect.size.height = 2 * radius;
[[NSColor redColor] set];
[[NSBezierPath bezierPathWithOvalInRect:dotRect] fill];
}
// Views which totally redraw their whole bounds without needing any of the
// views behind it should override isOpaque to return YES. This is a performance
// optimization hint for the display subsystem.
- (BOOL)isOpaque {
return YES;
}
@end