Skip navigation.
 
mlNSClipView leaves background artifacts when drawing
FROM : Dave Fernandes
DATE : Sun Nov 11 19:25:04 2007

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];
   }
}

Related mailsAuthorDate
No related mails found.