NSSlider NSSliderCell 裁剪自定义旋钮
我正在使用自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSSLider 期望每个控件尺寸的旋钮图像具有特殊尺寸:
不幸的是,旋钮图像的高度不得超过此参数之一。但它的宽度可能会更长。如果是,您可以像这样计算旋钮的 x 位置:
其中 _barRect 是条背景的 cellFrame,来自:
我已经为自定义 NSSlider 创建了一个简单的解决方案。点击此链接
https://github.com/Doshipak/LADSlider
The NSSLider expects a special sizes off the knob images for each control size:
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:
Where _barRect is a cellFrame of your bar background from:
I've created a simple solution for the custom NSSlider. Follow this link
https://github.com/Doshipak/LADSlider
除了
[NSSliderCell DrawKnob:]
之外,您还可以覆盖[NSSliderCell KnobRectFlipped:]
。这是我的解决方案:
You can override
[NSSliderCell knobRectFlipped:]
in addition to[NSSliderCell drawKnob:]
.Here is my solution:
我知道已经有一段时间了,但我自己也遇到了这个问题,并找到了一个快速而肮脏的解决方法。
我无法解决最初的原因,但似乎 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.
您不需要从单元格绘图方法内部锁定和解锁 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?