NSClipView leaves background artifacts when drawing

  • Hi All,
    I'm having some trouble trying to draw a moving rectangle on a custom
    view contained inside an NSScrollView. The problem is that while the
    rectangle draws fine, there is always a 1 pixel border around the
    newly drawn area that is transparent, showing the NSClipView's
    background color. The transparent border is always 1 pixel,
    regardless of my scale set using scaleUnitSquareToSize.

    The custom view drawing method, and the method to update the
    rectangle position are below. The setCursor method is called by an
    NSTimer callback.

    I'm not even sure where to begin searching the archives for this one.

    Dave

    // Do the drawing.
    - (void)drawRect:(NSRect)rect
    {
    [self lockFocus];
    [NSGraphicsContext saveGraphicsState];
    NSGraphicsContext* gContext = [NSGraphicsContext currentContext];
    [[[self enclosingScrollView] contentView] setBackgroundColor:
    [NSColor yellowColor]];
    [gContext setShouldAntialias:NO];
    //[[NSGraphicsContext currentContext]
    setCompositingOperation:NSCompositeSourceOver];

    // Erase the background.
    [[NSColor whiteColor] set];
    [NSBezierPath fillRect:rect];

    // Draw the cursor.
    if (drawCursor)
    {
      [[[NSColor greenColor] colorWithAlphaComponent:0.3] set];
      [NSBezierPath fillRect:cursorRect];
    }

    [NSGraphicsContext restoreGraphicsState];
    [self unlockFocus];
    }

    // Set the current cursor position.
    - (void)setCursor:(float)leftEdge width:(float)width
    {
    // Flag old cursor rect to be redrawn.
    if (drawCursor)
    {
      [self setNeedsDisplayInRect:cursorRect];
    }

    // Set location of new cursor.
    drawCursor = width > 0.;

    if (drawCursor)
    {
      cursorRect = [self bounds];
      float xSlope = [self bounds].size.width / [sound duration];

      cursorRect.origin.x = leftEdge * xSlope;
      cursorRect.size.width = width * xSlope;

      [self setNeedsDisplayInRect:cursorRect];
    }
    }
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today