4

如何创建与系统偏好设置中的滑块完全一样的自定义 NSSlider -> 桌面和屏幕保护程序 -> 屏幕保护程序 -> 启动屏幕保护程序:?

我尝试使用覆盖的 continueTracking: 子类 NSSliderCell:但它没有按预期工作。

4

1 回答 1

3

我玩了一会儿,至少从一个子类开始有了一个很好的开始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

基本上,在 期间-continueTracking:at:inView:,它会检查它与刻度线的距离,如果足够接近,它会打开仅允许刻度线值的选项。这会导致它捕捉到刻度线,然后一旦你离得足够远,你就会关闭“仅刻度线”选项,直到你足够接近另一个刻度线。

于 2011-04-30T19:28:01.273 回答