0
我有一些麻煩理解如何連線一個NSMenuItem自定義NSView支持動畫和拖放。我有NSView的以下子類處理大部分作業。它會在應用程序正確啓動時繪製我的圖標,但是當我從另一個調用者調用setIcon函數時,我無法正確設置子視圖以進行更改。我缺少一些設計元素嗎?如何在NSStatusItem的自定義NSView中重繪圖像?
TrayIconView.m
#import "TrayIconView.h"
@implementation TrayIconView
@synthesize statusItem;
static NSImageView *_imageView;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
statusItem = nil;
isMenuVisible = NO;
_imageView = [[NSImageView alloc] initWithFrame:[self bounds]];
[self addSubview:_imageView];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Draw status bar background, highlighted if menu is showing
[statusItem drawStatusBarBackgroundInRect:[self bounds]
withHighlight:isMenuVisible];
}
- (void)mouseDown:(NSEvent *)event {
[[self menu] setDelegate:self];
[statusItem popUpStatusItemMenu:[self menu]];
[self setNeedsDisplay:YES];
}
- (void)rightMouseDown:(NSEvent *)event {
// Treat right-click just like left-click
[self mouseDown:event];
}
- (void)menuWillOpen:(NSMenu *)menu {
isMenuVisible = YES;
[self setNeedsDisplay:YES];
}
- (void)menuDidClose:(NSMenu *)menu {
isMenuVisible = NO;
[menu setDelegate:nil];
[self setNeedsDisplay:YES];
}
- (void)setIcon:(NSImage *)icon {
[_imageView setImage:icon];
}
TrayIconView.h
#import <Cocoa/Cocoa.h>
@interface TrayIconView : NSView
{
BOOL isMenuVisible;
}
@property (retain, nonatomic) NSStatusItem *statusItem;
- (void)setIcon:(NSImage *)icon;
@end
創建狀態項目在哪裏? –
嘗試在setIcon中放置一個setNeedsDisplay ...不太可能解決您的問題,但請試一試。 –