Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MDMStudio

macrumors newbie
Original poster
Jun 26, 2010
5
0
Hello, I am trying to draw in a grid in a NSScrollView, it works fine when the scrollView is resized, however, if the view contained within the NSScrollView changes size, and you then scroll, the view doesn't get redrawn correctly, and only part of the grid draws. Here's my code:


Code:
#define kGridSpace     15.0f
#define kGridLineSize     0.3f



- (void)drawRect:(NSRect)rect {


// Our path
    NSBezierPath *path = [NSBezierPath bezierPath];

// The scale factor
CGFloat scaleFactor = [window userSpaceScaleFactor];

// The y beginning and end coords
CGFloat dypoint = 0.0;
CGFloat eypoint = rect.size.height * scaleFactor;

// the x beginning and end coords
CGFloat dxpoint = 0.0;
CGFloat expoint = rect.size.width * scaleFactor;

[[NSColor whiteColor] setStroke];
[path setLineWidth:kGridLineSize];

BOOL continueDrawing = TRUE;
while (continueDrawing) {
// Create our line.
[path moveToPoint:NSMakePoint(0.0f, dypoint)];
[path lineToPoint:NSMakePoint(expoint, dypoint)];
[path closePath];
// Add our drawing point reference
dypoint += kGridSpace;
// Are we done drawing?
if (dypoint > eypoint)
continueDrawing = FALSE;
}

continueDrawing = TRUE;
while (continueDrawing) {
// Create our line.
[path moveToPoint:NSMakePoint(dxpoint, 0.0f)];
[path lineToPoint:NSMakePoint(dxpoint, eypoint)];
[path closePath];
// Add our drawing point reference
dxpoint += kGridSpace;
// Are we done drawing?
if (dxpoint > expoint)
continueDrawing = FALSE;

}
[path stroke];
}


On a side note, if I use path -fill, it won't draw.
 
Instead of using rect, use [self bounds]. rect is what part of the view needs to be redrawn, and is usually only used if you have sophisticated drawing that needs to efficiently redraw.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.