NSSlider NSSliderCell 裁剪自定义旋钮

发布于 2024-10-14 13:39:58 字数 1204 浏览 9 评论 0原文

我正在使用自定义 NSSliderCell 创建自定义 NSSlider。除了旋钮之外,一切都运转良好。当我将其拖动到最大值时,旋钮将被剪切,我只能看到旋钮图像的 50%。

当分配我的自定义 NSSliderCell 时,我将旋钮厚度设置为我用作旋钮的图像的宽度。我假设(我猜错了)它会考虑到这一点并阻止它被剪辑?

有什么想法我做错了吗?仅当旋钮被限制在 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];

}

I am creating a custom NSSlider with a custom NSSliderCell. All is working beautifully, other than the knob. When I drag it to the max value the knob is being clipped, I can only see 50% of the knob image.

When assigning my custom NSSliderCell I am setting the knobThickness to the width of the image I am using as the knob. I assumed (I guess wrongly) that it would take that into account and stop it from clipping?

Any ideas what I am doing wrong? The slider is hitting the maxValue only when the knob is clipped at 50%, so its not travelling without adding any value.

- (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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人│生佛魔见 2024-10-21 13:39:58

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

The NSSLider expects a special sizes off the knob images for each control size:

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

Unfortunately the height of your knob image mustn't exceed one of this parameters. But it's width may be longer. If it is you may count an x position for the knob like this:

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

Where _barRect is a cellFrame of your bar background from:

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

I've created a simple solution for the custom NSSlider. Follow this link
https://github.com/Doshipak/LADSlider

一笑百媚生 2024-10-21 13:39:58

除了 [NSSliderCell DrawKnob:] 之外,您还可以覆盖 [NSSliderCell KnobRectFlipped:]
这是我的解决方案:

- (void)drawKnob:(NSRect)rect
{
  NSImage *drawImage = [self knobImage];

  NSRect drawRect = [self knobRectFlipped:[self.controlView isFlipped]];

  CGFloat fraction = 1.0;

  [drawImage drawInRect:drawRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:fraction respectFlipped:YES hints:nil];
}

- (NSRect)knobRectFlipped:(BOOL)flipped
{
  NSImage *drawImage = [self knobImage];

  NSRect drawRect = [super knobRectFlipped:flipped];

  drawRect.size = drawImage.size;

  NSRect bounds = self.controlView.bounds;
  bounds = NSInsetRect(bounds, ceil(drawRect.size.width / 2), 0);
  CGFloat val = MIN(self.maxValue, MAX(self.minValue, self.doubleValue));
  val = (val - self.minValue) / (self.maxValue - self.minValue);
  CGFloat x = val * NSWidth(bounds) + NSMinX(bounds);

  drawRect = NSOffsetRect(drawRect, x - NSMidX(drawRect) + 1, 0);
  return drawRect;
}

You can override [NSSliderCell knobRectFlipped:] in addition to [NSSliderCell drawKnob:].
Here is my solution:

- (void)drawKnob:(NSRect)rect
{
  NSImage *drawImage = [self knobImage];

  NSRect drawRect = [self knobRectFlipped:[self.controlView isFlipped]];

  CGFloat fraction = 1.0;

  [drawImage drawInRect:drawRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:fraction respectFlipped:YES hints:nil];
}

- (NSRect)knobRectFlipped:(BOOL)flipped
{
  NSImage *drawImage = [self knobImage];

  NSRect drawRect = [super knobRectFlipped:flipped];

  drawRect.size = drawImage.size;

  NSRect bounds = self.controlView.bounds;
  bounds = NSInsetRect(bounds, ceil(drawRect.size.width / 2), 0);
  CGFloat val = MIN(self.maxValue, MAX(self.minValue, self.doubleValue));
  val = (val - self.minValue) / (self.maxValue - self.minValue);
  CGFloat x = val * NSWidth(bounds) + NSMinX(bounds);

  drawRect = NSOffsetRect(drawRect, x - NSMidX(drawRect) + 1, 0);
  return drawRect;
}
梦亿 2024-10-21 13:39:58

我知道已经有一段时间了,但我自己也遇到了这个问题,并找到了一个快速而肮脏的解决方法。

我无法解决最初的原因,但似乎 NSSlider 期望得到二次手柄图像。
我发现最简单的方法是将滑块的范围设置为 0.0f - 110.0f。

然后检查分配的 valueChanged 目标方法是否该值 > 100.0f 并将其设置回该值(如果是)。我创建了一个背景图像,其中右侧有一些仅包含 alpha 像素的像素,因此您的背景不会比实际的推子范围更宽。

快速而肮脏,但不需要大量代码并且工作得很好。
希望这可以帮助其他遇到同样问题的人。

Know it's been awhile but I ran into this issue myself and found a quick-and-dirty workaround.

I couldn't get around the initial reason for this but it seems that NSSlider is expecting a quadratic handle image.
The easiest way I found was to set the range of your slider to be from 0.0f - 110.0f for example.

Then you check in the valueChanged target method assigned if the value is > 100.0f and set it back to that value if it is. I created a background image with some pixels of alpha-only pixels on the right side so your background isn't wider than the actual fader range.

Quick-and-dirty but doesn't require a lot code and works pretty well.
Hope this helps other guys stumbling upon the same issue.

晚雾 2024-10-21 13:39:58

您不需要从单元格绘图方法内部锁定和解锁 controlView 的焦点。这些方法仅由 controlView 的 -drawRect: 方法调用,该方法在视图焦点锁定时调用。

为什么要在 -drawKnob 中合成旋钮图像的 Y 坐标上添加 20 个点?

You don’t need to lock and unlock focus on the controlView from inside cell drawing methods. These methods are only called by your controlView’s -drawRect: method, which is called with the view’s focus locked.

Why are you adding 20 points to the Y coordinate the knob image is composited to in -drawKnob?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文