我已經實現了一個NSButton的圖像。NSButtonCell懸停圖像,點擊透明背景?
當用戶將它懸停時,圖像變爲其他內容,然後重新打開。
正常狀態:
懸停:
的我使用的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」的圓形圖像和透明背景),但當用戶點擊它時,它會顯示一個「白色」背景,而不會保留舊的「透明「背景
- 我應該如何解決這個問題?
http://stackoverflow.com/questions/7756312/nsbutton-white-background-when-clicked,HTTP ://stackoverflow.com/questions/4801418/nsbuttons-images-lose-transparency-on-selection,將按鈕單元格類型設置爲「瞬間更改」是一個足夠的解決方案嗎? – Vervious 2012-04-21 15:52:52