我有一个无边框的 NSButton,它包含一个图像,并且是我的 ButtonEffect 类的子类,以检测 mouseEntered 和 mouseExited 事件。我的目标是在鼠标进入按钮区域时让图像的边缘发光。我知道我可以通过使用两个不同的图像(一个有阴影,另一个没有)来实现这一点,但我需要用一个图像来做到这一点。
这是我希望为 mouseEntered 事件实现的示例:
这是光标离开按钮区域后的样子:
这是我通过设置单元格背景的尝试,但它改变了整个按钮区域的颜色。
#import "ButtonEffect.h"
@interface ButtonEffect ()
@property NSTrackingArea *trackingArea;
@end
@implementation ButtonEffect
- (void)updateTrackingAreas {
[super updateTrackingAreas];
if (_trackingArea) {
[self removeTrackingArea:_trackingArea];
}
NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
_trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:options owner:self userInfo:nil];
[self addTrackingArea:_trackingArea];
}
- (void)mouseEntered:(NSEvent *)event {
[[NSCursor pointingHandCursor] set];
[self.cell setBackgroundColor:NSColor.redColor];
NSLog(@"mouse entered button");
}
- (void)mouseExited:(NSEvent *)event {
[[NSCursor arrowCursor] set];
[self.cell setBackgroundColor:nil];
NSLog(@"mouse exited button");
}
@end