Custom NSSliderCell

I kind of performed the implementation of a custom slider cell that can draw using images for the scroll bar and pen. The only obstacle that is now on the way is when I quickly drag the handle, the images are messed up. I posted a screenshot.

Screen shot

Here is the code:

#import "customSliderCell.h" @implementation customSliderCell - (void)drawKnob:(NSRect)knobRect { NSImage * knob = knobImage; [[self controlView] lockFocus]; [knob compositeToPoint:NSMakePoint(knobRect.origin.x,knobRect.origin.y+knobRect.size.height) operation:NSCompositeSourceOver]; [[self controlView] unlockFocus]; } - (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped { rect.size.height = 8; NSRect leftRect = rect; leftRect.origin.x=0; leftRect.origin.y=2; leftRect.size.width = knobrect.origin.x + (knobrect.size.width); [leftBarImage setSize:leftRect.size]; [leftBarImage drawInRect:leftRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1]; NSRect rightRect = rect; rightRect.origin.x=0; rightRect.origin.y=2; rightRect.origin.x = knobrect.origin.x; [rightBarImage setSize:rightRect.size]; [rightBarImage drawInRect:rightRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1]; } 

Ah, I'm so close. any help regarding why this is happening and how to solve it will be greatly appreciated, thanks!

+7
objective-c cocoa nsslider
source share
4 answers

Well, that’s how it turned out. Apparently, the slider was trying to be smart and draw only where the pen was. therefore, apparently, I have to make corrections invalidated all the time by overriding setNeedsDisplayInRect in the slider class.

 #import "customSlider.h" @implementation customSlider -(void)setNeedsDisplayInRect:(NSRect)invalidRect{ [super setNeedsDisplayInRect:[self bounds]]; } @end 
+13
source share

I am new to Objective-c. I also ran into this problem! Here is the solution to find that I spent two days))) Save and restore GraphicsState:

 [NSGraphicsContext restoreGraphicsState]; //... [leftBarImage drawInRect:leftRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1]; //... [rightBarImage drawInRect:rightRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1]; [NSGraphicsContext saveGraphicsState]; 

Sorry for the bad english.

+4
source share

Delete all -lockFocus and -unlockFocus . The framework will take care of setting the drawing context for you before -drawBarInside:flipped: or -drawKnob: is sent.

In addition, you should not create objects in the drawing method.

+2
source share

Ha, this is another story. No, NSResponder is right, and you must remove all things lockFocus, however this problem is the result of the default slider created by NSSliderCell somewhere outside for drawBarInside: flipped: flipped. I ran into this problem not so long ago.

Here is a discussion and solution: http://www.cocoabuilder.com/archive/cocoa/177288-preventing-nsslider-bar-from-drawing.html , in a word, you can override the whole drawCell: inView: or use the "dirty trick" "with overriding a private method. I personally don't like hacks, but in this case I did

 - (BOOL)_usesCustomTrackImage { return YES; } 

And he solved the problem for me

+1
source share

All Articles