子类NSProgressIndicator [英] Subclass NSProgressIndicator
问题描述
- (void)drawRect:(NSRect)dirtyRect {
NSRect rect = NSInsetRect([self bounds], 1.0, 1.0);
CGFloat radius = rect.size.height / 2;
NSBezierPath *bz = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
[bz setLineWidth:2.0];
[[NSColor blackColor] set];
[bz stroke];
rect = NSInsetRect(rect, 2.0, 2.0);
radius = rect.size.height / 2;
bz = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
[bz setLineWidth:1.0];
[bz addClip];
rect.size.width = floor(rect.size.width * ([self doubleValue] / [self maxValue]));
NSRectFill(rect);
应用启动时,其外观如下所示:
但是在复制过程中,旧的条会出现.
怎么了?
似乎进度条的进度未在drawRect:
中绘制,因此仅覆盖drawRect:
是不够的.但是,如果将进度条图层作为后备,则您有责任进行所有绘制.
从 i like to subclass a NSProgressIndicator. I've used this code and i set the Subclass in the Interface Builder: When the app starts its looks like this:
But during the copy progress the old bar shows up.
Whats wrong? It seems that the progress bar's progress is not drawn in From the documentation: The view class automatically creates a backing layer for you (using
Check the "Core Animation Layer" in IB or add this to your sub class:
这篇关于子类NSProgressIndicator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!- (void)awakeFromNib {
[super awakeFromNib];
[self setWantsLayer:YES];
}
- (void)drawRect:(NSRect)dirtyRect {
NSRect rect = NSInsetRect([self bounds], 1.0, 1.0);
CGFloat radius = rect.size.height / 2;
NSBezierPath *bz = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
[bz setLineWidth:2.0];
[[NSColor blackColor] set];
[bz stroke];
rect = NSInsetRect(rect, 2.0, 2.0);
radius = rect.size.height / 2;
bz = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
[bz setLineWidth:1.0];
[bz addClip];
rect.size.width = floor(rect.size.width * ([self doubleValue] / [self maxValue]));
NSRectFill(rect);
drawRect:
, so just overriding drawRect:
is not enough. However, if you make the progress bar layer backed, you are responsible for doing all the drawing.
makeBackingLayer
if overridden), and you must use the view class’s
drawing mechanisms.- (void)awakeFromNib {
[super awakeFromNib];
[self setWantsLayer:YES];
}