开发者

Cocoa: Subclassing NSProgressIndicator on Lion

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开发者_如何学C -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.

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?


How about making the progress indicator layer-backed and applying a Core Image filter to recolour it?


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:

#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];    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜