如何自定义NSSlider [英] How to customize a NSSlider

查看:241
本文介绍了如何自定义NSSlider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用5个值在Cocoa中实现自定义滑块.请参阅我的演示项目,可以在这里下载: http://s000.tinyupload.com/index.php?file_id=07311576247413689572 .

I'm trying to implement a custom slider in Cocoa with 5 values. See my demo project, which can be downloaded here: http://s000.tinyupload.com/index.php?file_id=07311576247413689572.

我将 NSSliderCell 子类化,并实现了drawKnob:(NSRect)knobRectdrawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped等方法.

I've subclassed the NSSliderCell and implemented methods like drawKnob:(NSRect)knobRect and drawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped etc.

我遇到了一些问题:

  1. 我无法相对于背景图像正确放置旋钮.我知道我可以更改旋钮的框架,并且我尝试进行一些计算以正确定位旋钮,但是无法使其适用于我的自定义滑块.有人可以帮我吗?
  2. 自定义滑块背景的高度为41px.在drawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped中,我也将框架的高度也更改为41px,但是整个背景都不可见.为什么?
  3. 我注意到所包含的图像(背景和旋钮)是垂直翻转的.为什么?请注意,边框的顶部在背景中比边框的底部要暗,但是当我绘制背景时,这种情况就相反了.
  1. I'm not able to position the knob correctly regarding to the background image. I know that I'm able to change the knob's frame, and I've tried doing some calculation to position the knob correctly, but I'm not able to make it work for my custom slider. Could someone please help me with this?
  2. The height of my custom slider background is 41px. In the drawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped I change the height of the frame to 41px as well, but the entire background is not visible. Why?
  3. I've noticed that the included images (the background and knob) are flipped vertically. Why? Note that the border top is darker in the background compared to the bottom, but this is reversed when I draw the background.

推荐答案

  1. 我在计算旋钮矩形的x位置时发现了一个错误:您在应使用宽度的位置上使用了图像的高度.

  1. I found a mistake in your calculation of the x position of the knob rectangle: You used the height of the image where you should have used the width.

单元格图形被剪切到控件的框架.也许您可以在单元格唤醒时扩展控制框架.

The cell drawing is being clipped to the frame of the control. Maybe you could expand the control frame when your cell awakes.

您需要使用NSImage方法drawInRect:fromRect:operation:fraction:respectFlipped:hints:,并为respectFlipped:参数传递YES. Apple的控件通常确实使用翻转的坐标.

You need to use the NSImage method drawInRect:fromRect:operation:fraction:respectFlipped:hints:, and pass YES for the respectFlipped: parameter. Apple's controls generally do use flipped coordinates.

已添加:扩展awakeFromNib中的框架似乎不起作用,框架被调回.这是行得通的.代替覆盖drawBarInside:flipped:,添加以下覆盖:

Added: Expanding the frame in awakeFromNib doesn't seem to work, the frame gets set back. Here's something that does work. Instead of overriding drawBarInside:flipped:, add this override:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSRect controlFrame = [controlView frame];
    float bgHeight = self.backgroundImage.size.height;
    if (controlFrame.size.height < bgHeight)
    {
        controlFrame.size.height = bgHeight;
        [controlView setFrame: controlFrame];
    }

    [self.backgroundImage
        drawInRect: [controlView bounds]
        fromRect: NSZeroRect
        operation: NSCompositeSourceOver
        fraction: 1.0
        respectFlipped: YES
        hints: NULL];
    [self drawKnob];
}

这篇关于如何自定义NSSlider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆