2013-02-06 32 views
2

我需要一個類似於NSLevelIndicator的控件,在NSContinuousCapacityLevelIndicatorStyle中,但是具有反色。與NSLevelIndicator,顏色是這樣的: 綠色直到警告級別,黃色從警告到臨界級別,紅色從臨界級別向前。這對音量控制來說很好。但是我有一個對應於填充氣罐的值:我想要一個滿罐的綠色,黃色表示警告,紅色表示空。 我還沒有找到任何手段來改變顏色NSLevelIndicator。因此,在我開始編寫我自己的自定義控件之前,有沒有一個NSControl可用的地方已經做了我想要的(當然,我在問之前使用了google搜索,但無濟於事)?是否有一個類似NSLevelIndicator的自定義控件與反向着色?

感謝您的閱讀。

回答

1

NSLevelIndicator(至少目前)爲您自動做這個,如果你設定高於臨界水平高的警告級別!

+0

就是這樣!謝謝,我從來沒有想出這個想法。 – Dirk

1

子類NSLevelIndicator,寫自己的- (void)drawRect:(NSRect)theRect

#import <Cocoa/Cocoa.h> 


@interface PBLevelIndicator : NSLevelIndicator { 

    unsigned int mPercent; 
} 


@end 

#import "PBLevelIndicator.h" 


@implementation PBLevelIndicator 
- (void)drawRect:(NSRect)theRect 
{ 
    NSRect fillingRect = theRect; 
    fillingRect.size.width = theRect.size.width*mPercent/100; 
    NSColor *indicatorColor; 

    if(mPercent >= 99) 
    { 
     indicatorColor = [NSColor greenColor]; 
    } 
    else if (mPercent >50) 
    { 
     indicatorColor = [NSColor yellowColor]; 
    } 
    else 
    { 
     indicatorColor = [NSColor redColor]; 
    } 
    [indicatorColor set]; 
    NSRectFill(fillingRect); 
} 

-(void) setPercentage:(unsigned int) inPercent 
{ 
    mPercent = inPercent; 
    [self setNeedsDisplay:YES]; 
} 
@end 
+0

這是個好主意,謝謝。我試過了,但是新的類不是符合編碼的關鍵值。我會更深入地研究這個問題,看看我是否得到解決。 – Dirk

相關問題