子类NSProgressIndicator

Kon*_*obi 5 macos cocoa subclass nsprogressindicator

我喜欢子类化NSProgressIndicator.我已经使用了这段代码,并在Interface Builder中设置了Subclass:

- (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);
Run Code Online (Sandbox Code Playgroud)

当应用程序启动时,它看起来像这样: App开始

但在复制过程中,旧栏出现了. 复制期间

怎么了?

Mor*_*ast 2

似乎进度条的进度没有绘制在 中drawRect:,因此仅覆盖drawRect:是不够的。但是,如果您将进度条图层设置为支持,则您将负责完成所有绘图。

\n\n

文档中:

\n\n
\n

视图类会自动为您创建一个支持层( makeBackingLayer如果被覆盖,则使用\n),并且您必须使用视图类\xe2\x80\x99s\n 绘图机制。

\n
\n\n

检查IB中的“核心动画层”或将其添加到您的子类中:

\n\n
- (void)awakeFromNib {\n    [super awakeFromNib];\n    [self setWantsLayer:YES];\n}\n
Run Code Online (Sandbox Code Playgroud)\n