I am trying to implement a custom circular slider which uses images to draw the dial and the knob. For this purpose I have subclassed NSSliderCell and overridden the drawBarInside and drawKnob methods.
Now, if I leave the slider type as default (horizontal), my drawing functions are called and images are drawn in stead of the default bar and knob (of course it looks all wrong since images are made for a circular slider, but they are there). But as soon as I change the slider type to circular (either in IB or by setting self.sliderType = NSCircularSlider; in my slider cell) those two methods are never called and my images are not drawn (only the standard dial is created).
Am I forgetting something here, or can circular sliders not be customized at all?
Here is my code:
DialSliderCell is a subclass of NSSliderCell and is initiated and set in a class called DialSlider which is a subclass of NSSlider (this class does nothing else at the time).
@implementation DialSliderCell
- (id)init {
self = [super init];
if (self) {
dialImage = [NSImage imageNamed:@"dial"];
knobImage = [NSImage imageNamed:@"dialKnob"];
self.sliderType = NSCircularSlider;
}
NSLog(@"init");
return self;
}
- (void)drawKnob:(NSRect)knobRect {
knobRect.size = knobImage.size;
[knobImage drawInRect:knobRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
NSLog(@"drawKnob");
}
- (void)drawBarInside:(NSRect)aRect flipped:(BOOL)flipped {
[dialImage drawInRect:aRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
NSLog(@"drawBarInside");
}
@end
I have solved this problem by overriding the
drawInteriorWithFramemethod. The solution is based on the original method. I just removed anything not related to circular sliders, removed the original graphics and added my own. The geometry is almost entirely original.The size of the
cellFramerectangle is set to the size of thedialImageimage.Notice that the method above doesn’t seem to get called automatically so I also had to override the
drawWithFramemethod and make it calldrawInteriorWithFrame: