子类NSProgressIndicator [英] Subclass NSProgressIndicator

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

问题描述

我喜欢将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:是不够的.但是,如果将进度条图层作为后备,则您有责任进行所有绘制.

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setWantsLayer:YES];
}

i like to subclass a NSProgressIndicator. I've used this code and i set the Subclass in the Interface Builder:

- (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);

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 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.

From the documentation:

The view class automatically creates a backing layer for you (using makeBackingLayer if overridden), and you must use the view class’s drawing mechanisms.

Check the "Core Animation Layer" in IB or add this to your sub class:

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setWantsLayer:YES];
}

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

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