我已经实现了一个带有图像的 NSButton。
当用户将其悬停时,图像会更改为其他内容,然后重新显示。
正常状态:
悬停时:
我用于的代码NSButtonCell
是:
界面 :
#import <Foundation/Foundation.h>
@interface DKHoverButtonCell : NSButtonCell
{
NSImage *_oldImage;
NSImage *hoverImage;
}
@property (retain) NSImage *hoverImage;
@end
执行 :
#import "DKHoverButtonCell.h"
@interface NSButtonCell()
- (void)_updateMouseTracking;
@end
@implementation DKHoverButtonCell
@synthesize hoverImage;
- (void)mouseEntered:(NSEvent *)event {
if (hoverImage != nil && [hoverImage isValid]) {
_oldImage = [[(NSButton *)[self controlView] image] retain];
[(NSButton *)[self controlView] setImage:hoverImage];
}
}
- (void)mouseExited:(NSEvent *)event {
if (_oldImage != nil && [_oldImage isValid]) {
[(NSButton *)[self controlView] setImage:_oldImage];
[_oldImage release];
_oldImage = nil;
}
}
- (void)_updateMouseTracking {
[super _updateMouseTracking];
if ([self controlView] != nil && [[self controlView] respondsToSelector:@selector(_setMouseTrackingForCell:)]) {
[[self controlView] performSelector:@selector(_setMouseTrackingForCell:) withObject:self];
}
}
- (void)setHoverImage:(NSImage *)newImage {
[newImage retain];
[hoverImage release];
hoverImage = newImage;
[[self controlView] setNeedsDisplay:YES];
}
- (void)dealloc {
[_oldImage release];
[hoverImage release];
[super dealloc];
}
@end
现在,这里是问题:
- 尽管上述控件 100% 工作(带有“X”圆形图像和透明背景),但当用户单击它时,它会显示“白色”-ish 背景,而不是保留我旧的“透明”背景
- 我应该如何解决这个问题?