objective-c – NSSlider NSSliderCell剪辑自定义旋钮

我正在创建一个带有自定义NSSliderCell的自定义NSSlider.除旋钮外,一切都很漂亮.当我将其拖动到最大值时,旋钮被剪裁,我只能看到旋钮图像的50%.

在分配我的自定义NSSliderCell时,我将knobThickness设置为我用作旋钮的图像的宽度.我假设(我猜错了)它会考虑到并阻止剪辑?

我有什么想法我做错了吗?仅当旋钮被剪裁为50%时,滑块才会达到maxValue,因此在没有添加任何值的情况下它不会移动.

- (void)drawKnob:(NSRect)knobRect {
 NSImage * knob = _knobOff;
 knobRectVar = knobRect;

 [[self controlView] lockFocus];
 [knob
  compositeToPoint:
  NSMakePoint(knobRect.origin.x+4,knobRect.origin.y+knobRect.size.height+20)
  operation:NSCompositeSourceOver];
 [[self controlView] unlockFocus];
}

- (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped {
 rect.size.height = 8;

 [[self controlView] lockFocus];
 NSImage *leftCurve = [NSImage imageNamed:@"customSliderLeft"];
 [leftCurve drawInRect:NSMakeRect(5, 25, 8, 8) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];

    NSRect leftRect = rect;
    leftRect.origin.x=13;
    leftRect.origin.y=25;
    leftRect.size.width = knobRectVar.origin.x + (knobRectVar.size.width/2);
    [leftBarImage setSize:leftRect.size];
    [leftBarImage drawInRect:leftRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
 [[self controlView] unlockFocus];

}

最佳答案 NSSLider希望每个控件尺寸的旋钮图像都有特殊尺寸:

NSRegularControlSize:   21x21
NSSmallControlSize:     15x15
NSMiniControlSize:      12x12

不幸的是,旋钮图像的高度不得超过其中一个参数.但它的宽度可能会更长.如果是,你可以像这样计算旋钮的x位置:

CGFloat newOriginX = knobRect.origin.x *
        (_barRect.size.width - (_knobImage.size.width - knobRect.size.width)) / _barRect.size.width;

_barRect是您的酒吧背景的cellFrame:

- (void)drawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped;

我为自定义NSSlider创建了一个简单的解决方案.点击此链接
https://github.com/Doshipak/LADSlider

点赞