gpt4 book ai didi

objective-c - NSButtonCell 悬停图像,点击透明背景?

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:33 26 4
gpt4 key购买 nike

我实现了一个带有图像的 NSButton。

当用户悬停它时,图像会变成其他东西,然后再变回来。


正常状态:

enter image description here

悬停时:

enter image description here


我为 NSButtonCell 使用的代码是:

接口(interface):

#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”圆形图像和透明背景),但当用户点击它时,它会显示“白色”背景,而不是保留我原来的“透明”背景
  • 我应该如何解决这个问题?

最佳答案

您需要将按钮的类型更改为Momentary Change。您可以在属性检查器中更改它:

how to do it

或以编程方式更改:

[button setButtonType:NSMomentaryChangeButton];

关于objective-c - NSButtonCell 悬停图像,点击透明背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10258347/

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