Hot to create a custom NSSlider, such as the "Start-up Screen Saver:" slider in the system settings

How to create a custom NSSlider that works exactly like a slider in System Preferences β†’ Desktop and Screensaver β†’ Screensaver β†’ Screensaver :?

I tried to subclass NSSliderCell with overridden continueTracking: but it does not work properly.

+4
source share
1 answer

I played a little and at least started out pretty well with a subclass of NSSliderCell .

MDSliderCell.h :

 #import <Cocoa/Cocoa.h> @interface MDSliderCell : NSSliderCell { BOOL tracking; } @end 

MDSliderCell.m :

 #import "MDSliderCell.h" @implementation MDSliderCell - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView { if ([self numberOfTickMarks] > 0) tracking = YES; return [super startTrackingAt:startPoint inView:controlView]; } #define MD_SNAPPING 10.0 - (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView { if (tracking) { NSUInteger count = [self numberOfTickMarks]; for (NSUInteger i = 0; i < count; i++) { NSRect tickMarkRect = [self rectOfTickMarkAtIndex:i]; if (ABS(tickMarkRect.origin.x - currentPoint.x) <= MD_SNAPPING) { [self setAllowsTickMarkValuesOnly:YES]; } else if (ABS(tickMarkRect.origin.x - currentPoint.x) >= MD_SNAPPING && ABS(tickMarkRect.origin.x - currentPoint.x) <= MD_SNAPPING *2) { [self setAllowsTickMarkValuesOnly:NO]; } } } return [super continueTracking:lastPoint at:currentPoint inView:controlView]; } - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag { [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag]; } @end 

Basically, during -continueTracking:at:inView: it checks how close it is to the check mark, and if it's close enough, it includes an option that allows you to only check the check mark. This makes it snap to the check mark, and then when you go far enough, you enable the β€œcheck only” option until you get close to the other mark.

+3
source

All Articles