首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何给NSSlider上旋钮的左边部分上色

如何给NSSlider上旋钮的左边部分上色
EN

Stack Overflow用户
提问于 2012-11-20 00:03:16
回答 2查看 487关注 0票数 1

在我的项目中,NSSlider控制AVPlayer的音量。我想给旋钮左边的NSSlider部分涂上颜色。如何才能做到这一点?

EN

回答 2

Stack Overflow用户

发布于 2012-12-02 15:45:13

为此,您应该使用NSProgressIndicator

或者,您可以使用自定义NSSliderCell并覆盖- (BOOL)_usesCustomTrackImage以返回YES,并覆盖- (void)drawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped以绘制您的自定义条形图。在这里,您可以使用NSCell doubleValue来获取滑块的当前位置。

票数 1
EN

Stack Overflow用户

发布于 2013-10-06 15:37:46

您应该将NSSliderCell子类化,并编写类似下面的代码:

代码语言:javascript
复制
@interface CustomSliderCell : NSSliderCell {
    NSRect _barRect;
    NSRect _currentKnobRect;
}
//    You should set image for the barFill
//    (or not if you want to use the default background)
//    And image for the bar before the knob image
    @property (strong, nonatomic) NSImage *barFillImage;
    @property (strong, nonatomic) NSImage *barFillBeforeKnobImage;


//    Slider also has the ages so you should set
//    the different images for the left and the right one:
    @property (strong, nonatomic) NSImage *barLeftAgeImage;
    @property (strong, nonatomic) NSImage *barRightAgeImage;
@end

和实现:

代码语言:javascript
复制
- (void)drawKnob:(NSRect)knobRect {
    [super drawKnob:knobRect];
    _currentKnobRect = knobRect;
}

-(void)drawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped {
    _barRect = cellFrame; 
    NSRect beforeKnobRect = [self createBeforeKnobRect];
    NSRect afterKnobRect = [self createAfterKnobRect];

//    Draw bar before the knob
    NSDrawThreePartImage(beforeKnobRect, _barLeftAgeImage, _barFillBeforeKnobImage, _barFillBeforeKnobImage,
            NO, NSCompositeSourceOver, 1.0, flipped);

//    If you want to draw the default background 
//    add the following line at the at the beginning of the method:
//    [super drawBarInside:cellFrame flipped:flipped];
//    And comment the next line:
    NSDrawThreePartImage(afterKnobRect, _barFillImage, _barFillImage, _barRightAgeImage,
            NO, NSCompositeSourceOver, 1.0, flipped);
}

- (NSRect)createBeforeKnobRect {
    NSRect beforeKnobRect = _barRect;

    beforeKnobRect.size.width = _currentKnobRect.origin.x + _knobImage.size.width / 2;
    beforeKnobRect.size.height = _barFillBeforeKnobImage.size.height;
    beforeKnobRect.origin.y = beforeKnobRect.size.height / 2;

    return beforeKnobRect;
}

- (NSRect)createAfterKnobRect {
    NSRect afterKnobRect = _currentKnobRect;

    afterKnobRect.origin.x += _knobImage.size.width / 2;
    afterKnobRect.size.width = _barRect.size.width - afterKnobRect.origin.x;
    afterKnobRect.size.height = _barFillImage.size.height;
    afterKnobRect.origin.y = afterKnobRect.size.height / 2;

    return afterKnobRect;
}

我已经创建了LADSLider,它将帮助你简单而快速地创建你想要的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13457565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档