NSButton图像阴影对mouseEntered事件 [英] NSButton image shadow on mouseEntered event

查看:565
本文介绍了NSButton图像阴影对mouseEntered事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是一个例子,希望实现为mouseEntered事件:



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



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

  #importButtonEffect.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 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(@鼠标退出按钮);
}

@end


解决方案>

你想要这样的东西吗?

   - (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);

}


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.

Here's an example of what I hope to achieve for the mouseEntered event:

And here's what it should look after the cursor leaves the button area:

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

解决方案

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);

}

这篇关于NSButton图像阴影对mouseEntered事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆