Cocoa:在Lion上将NSProgressIndicator子类化 [英] Cocoa: Subclassing NSProgressIndicator on Lion

查看:257
本文介绍了Cocoa:在Lion上将NSProgressIndicator子类化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,驱使我疯了。我想在其Bar窗体中子类化 NSProgressIndicator ,以根据几个逻辑状态更改进度条的颜色。对我来说,我基本上像往常一样重写 -drawRect:。然而,一个非常奇怪的事情发生在狮子。即使我的 -drawRect 只是通过 [super drawRect:] 调用超类的实现,并且什么都不做将使用 狮子之前使用的样式显示整个进度条。如果你还记得,与狮子的进度栏风格更改了一个更平坦和光滑的外观从以前的玻璃状。

I have a problem that is driving me crazy. I want to subclass NSProgressIndicator in its "Bar" form to change the color of the progress bar based on a couple of logic states. For that i basically override the -drawRect: as usual. However a very strange thing happens on Lion. Even if my -drawRect just calls the superclass' implementation via [super drawRect:] and does nothing else at all the whole progress bar will be displayed using the style that was used before Lion. If you remember, with Lion the progress bar style has changes to a more flat and sleek look from the prior glassy one.

同样,只是覆盖 -drawRect 只做调用超类的实现,改变进度的风格酒吧到豹的。

Again, just overriding -drawRect to do nothing but to call the superclass' implementation changes the style of the progress bar to Leopard's. Does anyone have a slightest idea of what is going on and how i can fix this?

推荐答案

CustomeProgressIndicator.h:

CustomeProgressIndicator.h:

#import <Cocoa/Cocoa.h>

@interface CustomProgressIndicator : NSView {

    double m_minValue;
    double m_maxValue;
    double m_value;
}

- (void)setMaxValue:(double)newMaxValue;
- (void)setMinValue:(double)newMinValue;
- (void)setDoubleValue:(double)newDoubleValue;
- (void)drawRect:(NSRect)dirtyRect;   
@end

CustomProgressIndicator.m:

CustomProgressIndicator.m:

#import "CustomProgressIndicator.h"

@implementation CustomProgressIndicator

- (void)setMaxValue:(double)newMaxValue
{
    m_maxValue = newMaxValue;
}

- (void)setMinValue:(double)newMinValue
{
    m_minValue = newMinValue;
}

- (void)setDoubleValue:(double)newDoubleValue
{
    m_value = newDoubleValue;
    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor grayColor] set];
    [NSBezierPath fillRect:dirtyRect];

    double width = ((m_value - m_minValue) / (m_maxValue - m_minValue));
    CGRect bar = dirtyRect;
    bar.size.width *= width;

    [[NSColor darkGrayColor] set];
    [NSBezierPath fillRect:bar];    
}

这篇关于Cocoa:在Lion上将NSProgressIndicator子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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