NSScrollView clipping problem

Discussion in 'Mac Programming' started by MDMStudio, Jun 26, 2010.

  1. MDMStudio macrumors newbie

    Joined:
    Jun 26, 2010
    #1
    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.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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.
     

Share This Page