美文网首页
NSButton鼠标悬停、滑过、点击事件

NSButton鼠标悬停、滑过、点击事件

作者: MonkeyHan | 来源:发表于2017-10-31 14:32 被阅读400次

 AppKit和UIKit还是有很多区别的,国内的资源比较少,做Mac开发的人也很少,导致我学的过程中常常伴着各种翻墙到国外网站去找,之前我也发过相关的文章,有兴趣的同学可以看下。

说正题,我自己写了一个继承NSButton的类,处理了一些简单的东西。包括NSButton的鼠标悬停、滑过、点击更换图片,因为NSButton与UIButton不同,它没有UIControlEvent这个枚举,所以很多东西需要我们自己去写。直接上代码了

.m

#import "HSButton.h"

@implementation HSButton

{

NSString *imageName;

}

- (instancetype)initWithCoder:(NSCoder *)coder {

self = [super initWithCoder:coder];

if (self) {

imageName = self.image.name;

[HS_control setBtn:self textColor:[NSColor whiteColor]];

[self.cell setHighlightsBy:NSContentsCellMask];

self.alternateImage = [NSImage imageNamed:[NSString stringWithFormat:@"%@%@", imageName, @"_c"]];

// 设置图片铺满button

[self.cell setImageScaling:NSImageScaleAxesIndependently];

// 去掉button边框

[self setBordered:NO];

[self bezelStyle];

}

return self;

}

- (void)drawRect:(NSRect)dirtyRect {

[super drawRect:dirtyRect];

[HS_control setBtn:self textColor:[NSColor whiteColor]];

NSTrackingArea *area_V =[[NSTrackingArea alloc] initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited|NSTrackingActiveInKeyWindow owner:self userInfo:nil];

[self addTrackingArea:area_V];

// Drawing code here.

}

- (void)mouseExited:(NSEvent *)event {

[self setImage:[NSImage imageNamed:imageName]];

}

- (void)mouseEntered:(NSEvent *)event {

[self setImage:[NSImage imageNamed:[NSString stringWithFormat:@"%@%@", imageName, @"_m"]]];

}

//- (void)mouseDown:(NSEvent *)event {

//    NSLog(@"按下");

//    [self setImage:[NSImage imageNamed:[NSString stringWithFormat:@"%@%@", imageName, @"_c"]]];

//}

//

//- (void)mouseUp:(NSEvent *)event {

//    [self setImage:[NSImage imageNamed:[NSString stringWithFormat:@"%@%@", imageName, @"_m"]]];

//    NSLog(@"抬起");

//}

- (void)mouseMoved:(NSEvent *)event {

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

注意:我让公司美工做图的时候每个按钮给了我三种状态,命名统一规范(不嫌麻烦的话也可以自己命名),_c代表按下状态图片,_m代表滑过时候的状态图片。 eg:图片名称为 “myImage”,按下图片就为“myImage_c”,对应滑过图片就是 “myImage_m”,这样所有需要三种状态的按钮都会继承这个类。

其中上面代码中有两个我注释掉的方法mouseDown、mouseUp,分别为按下、抬起,起初我是想用这两个方法,改变button的背景图片,但是问题来了,这两个方法会和button的select方法冲突!!!这就蛋疼了,虽然可以用mouseDown代替select方法,但是就起不到封装的目的了,如果想判断这两个方法的发起时间也有方法,但是实在是麻烦,最后找到了这个属性alternateImage,虽然以前就知道,但是之前都不好使,原来它要搭配[self.cell setHighlightsBy:NSContentsCellMask];使用。

最后,因为公司要求的按钮比较统一,所以很多属性我都是直接写在了这个类里面,如果需要的同学可以单独写在外面赋值。

补充:代码中有一个方法,是改变button字体颜色的[HS_control setBtn:self textColor:[NSColor whiteColor]];


+ (void)setBtn:(NSButton *)btn textColor:(NSColor *)textColor

{

NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc]

initWithAttributedString:[btn attributedTitle]];

NSUInteger len = [attrTitle length];

NSRange range = NSMakeRange(0, len);

[attrTitle addAttribute:NSForegroundColorAttributeName

value:textColor

range:range];

[attrTitle fixAttributesInRange:range];

[btn setAttributedTitle:attrTitle];

}


如果有问题,欢迎交流  Demo 下载地址

相关文章

网友评论

      本文标题:NSButton鼠标悬停、滑过、点击事件

      本文链接:https://www.haomeiwen.com/subject/vnlbpxtx.html