mouseEntered事件上的NSButton图像阴影

时间:2022-11-05 08:17:23

I have a borderless NSButton that contains an image and is subclassed with my ButtonEffect class to detect mouseEntered and mouseExited events. My goal is to have just the edge of the image glow as the mouse enters the button area. I know I can achieve this by using two different images (one with shadow and another without) but I need to do this with one image.

我有一个无边框的NSButton,它包含一个图像,并使用我的ButtonEffect类进行子类化,以检测mouseEntered和mouseExited事件。我的目标是在鼠标进入按钮区域时让图像的边缘发光。我知道我可以通过使用两个不同的图像(一个带阴影,另一个没有)来实现这一点,但我需要用一个图像来做到这一点。

Here's an example of what I hope to achieve for the mouseEntered event: mouseEntered事件上的NSButton图像阴影

这是我希望为mouseEntered事件实现的一个示例:

And here's what it should look after the cursor leaves the button area: mouseEntered事件上的NSButton图像阴影

这是光标离开按钮区域后应该看到的内容:

Here's my attempt by setting the background of the cell but it changes the color of the entire button area.

这是我尝试设置单元格的背景但它会改变整个按钮区域的颜色。

#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

1 个解决方案

#1


1  

You want something like this?

你想要这样的东西吗?

     - (void)mouseEntered:(NSEvent *)event {
    CALayer *lay=[btn layer];
    CGColorRef myColor=CGColorCreateGenericRGB(1, 1, 1, 1);
    CGColorRelease(myColor);
    myColor=CGColorCreateGenericRGB(0, 0, 0.8, 1);
    [lay setBorderColor:myColor];
    [lay setBorderWidth:1];
    [btn setWantsLayer:YES];
    [btn setLayer:lay];
    CGColorRelease(myColor);

}

- (void)mouseExited:(NSEvent *)event {
    CALayer *lay=[btn layer];
    CGColorRef myColor=CGColorCreateGenericRGB(1, 1, 1, 1);
    CGColorRelease(myColor);
    myColor=CGColorCreateGenericRGB(0, 0, 0.8, 1);
    [lay setBorderColor:myColor];
    [lay setBorderWidth:0];
    [btn setWantsLayer:YES];
    [btn setLayer:lay];
    CGColorRelease(myColor);

}

#1


1  

You want something like this?

你想要这样的东西吗?

     - (void)mouseEntered:(NSEvent *)event {
    CALayer *lay=[btn layer];
    CGColorRef myColor=CGColorCreateGenericRGB(1, 1, 1, 1);
    CGColorRelease(myColor);
    myColor=CGColorCreateGenericRGB(0, 0, 0.8, 1);
    [lay setBorderColor:myColor];
    [lay setBorderWidth:1];
    [btn setWantsLayer:YES];
    [btn setLayer:lay];
    CGColorRelease(myColor);

}

- (void)mouseExited:(NSEvent *)event {
    CALayer *lay=[btn layer];
    CGColorRef myColor=CGColorCreateGenericRGB(1, 1, 1, 1);
    CGColorRelease(myColor);
    myColor=CGColorCreateGenericRGB(0, 0, 0.8, 1);
    [lay setBorderColor:myColor];
    [lay setBorderWidth:0];
    [btn setWantsLayer:YES];
    [btn setLayer:lay];
    CGColorRelease(myColor);

}