I have to redesign some controls. There is no Problem with NSButton, but NSSlider has drawing errors while I hold the mouse and move the knob around. If I release the mouse, it draws correctly.
This is the drawing code (my class inherits from NSSlider):
- (void)drawRect:(NSRect)dirtyRect
{
//[super drawRect:dirtyRect];
CGContextRef c;
CGFloat y, r, p;
c = [[NSGraphicsContext currentContext] graphicsPort];
r = (dirtyRect.size.height*0.8)/2.0;
p = (dirtyRect.size.width-r*2) * ([self doubleValue]*0.01);
y = dirtyRect.size.height/2.0;
CGContextSetLineWidth(c, 2);
CGContextSetStrokeColorWithColor(c, [_fullLineColor CGColor]);
CGContextMoveToPoint(c, 0, y);
CGContextAddLineToPoint(c, dirtyRect.size.width, y);
CGContextStrokePath(c);
CGContextSetLineWidth(c, 5);
CGContextSetStrokeColorWithColor(c, [_valueLineColor CGColor]);
CGContextMoveToPoint(c, 0, y);
CGContextAddLineToPoint(c, p, y);
CGContextStrokePath(c);
CGContextSetFillColorWithColor(c, [_knobColor CGColor]);
CGContextFillEllipseInRect(c, CGRectMake(p, y-r, r*2, r*2));
}
What can I do to avoid this behavior?
parameter dirtyRect
A rectangle defining the portion of the view that requires redrawing. This rectangle usually represents the portion of the view that requires updating. When responsive scrolling is enabled, this rectangle can also represent a nonvisible portion of the view that AppKit wants to cache.
You are drawing the control in dirtyRect. You should draw (the dirtyRect part of) the control in self.bounds.
Read more in the documentation of drawRect: