NSStatusItem 中的 NSProgressIndicator
Posted
技术标签:
【中文标题】NSStatusItem 中的 NSProgressIndicator【英文标题】:NSProgressIndicator in NSStatusItem 【发布时间】:2012-07-08 18:19:59 【问题描述】:我有一个带有自定义 NSView 的 NSStatusItem,它显示图像。 无论菜单是否打开,它都会显示不同的图像:
isMenuVisible = NO;
- (void)awakeFromNib
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem retain];
dragStatusView = [[DragStatusView alloc] init];
[dragStatusView retain];
dragStatusView.statusItem = statusItem;
[dragStatusView setMenu:statusMenu];
[dragStatusView setToolTip:NSLocalizedString(@"Menubar Countdown",
@"Status Item Tooltip")];
[statusItem setView:dragStatusView];
[dragStatusView setTitle:@"11"];
- (void)drawImage:(NSImage *)aImage centeredInRect:(NSRect)aRect
NSRect imageRect = NSMakeRect((CGFloat)round(aRect.size.width*0.5f-aImage.size.width*0.5f),
(CGFloat)round(aRect.size.height*0.5f-aImage.size.height*0.5f),
aImage.size.width,
aImage.size.height);
[aImage drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
- (void)drawRect:(NSRect)rect
// Draw status bar background, highlighted if menu is showing
[statusItem drawStatusBarBackgroundInRect:[self bounds]
withHighlight:isMenuVisible];
if(isMenuVisible)
[self drawImage:image2 centeredInRect:rect];
else
[self drawImage:image1 centeredInRect:rect];
(当然这不是全部,但我希望所有相关代码都能理解我的问题)
现在我想在这个 NSView(在这个 NSStatusItem 中)显示一个 NSProgressIndicator,如果上传正在进行,这意味着 1.设置上传开始时的NSProgressIndicator 2.收到了什么? ==> 隐藏 NSProgressIndicator 再次显示图像。
我将如何解决这个问题?
请帮助我。谢谢
【问题讨论】:
没人?我希望在这里找到可以帮助我的人。 【参考方案1】:这是基于视图的状态项的解决方案。
NSStatusBar *bar = [NSStatusBar systemStatusBar];
self.theItem = [bar statusItemWithLength:NSVariableStatusItemLength];
self.statusView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 40, 20)];
NSProgressIndicator* progress = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0, 0, 40, 20)];
[progress setIndeterminate:NO];
[self.statusView addSubview:progress];
self.theItem.view = self.statusView;
您还可以在基于菜单的状态项中自己绘制进度条 在进度更改时,您可以简单地更改状态项的图像。
- (void) updateIconWithProgress:(float)aProgress
NSImage* image = [NSImage imageNamed:@"small_icon_16_16.png"];
NSImage* img = [image copy];
[img lockFocus];
NSRect rect = NSMakeRect(2, 4, 12.0*aProgress, 8.0);
[[NSColor blueColor] set];
[NSBezierPath fillRect:rect];
[img unlockFocus];
[self.theItem setImage:img];
附言该代码是为 ARC(自动引用计数)编写的,因此没有保留或释放。
【讨论】:
以上是关于NSStatusItem 中的 NSProgressIndicator的主要内容,如果未能解决你的问题,请参考以下文章
Swift:10.10 中的 NSStatusItem 菜单行为(例如,仅在鼠标右键单击时显示)
在 NSStatusItem 中使用 NSProgressIndicator