gpt4 book ai didi

macos - NSProgressIndicator 子类

转载 作者:行者123 更新时间:2023-12-03 16:11:26 35 4
gpt4 key购买 nike

我喜欢子类化 NSProgressIndicator。我使用了这段代码,并在 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);

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

但是在复制过程中,旧的栏出现了。 During copying

怎么了?

最佳答案

看来进度条的进度不是在drawRect:中绘制的,所以仅仅重写drawRect:是不够的。但是,如果您将进度条图层设置为支持,则您将负责完成所有绘图。

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

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

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

关于macos - NSProgressIndicator 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18464814/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com