?AppKit和UIKit還是有很多區(qū)別的,國內(nèi)的資源比較少,做Mac開發(fā)的人也很少,導(dǎo)致我學(xué)的過程中常常伴著各種翻墻到國外網(wǎng)站去找,之前我也發(fā)過相關(guān)的文章,有興趣的同學(xué)可以看下。
說正題,我自己寫了一個(gè)繼承NSButton的類,處理了一些簡(jiǎn)單的東西。包括NSButton的鼠標(biāo)懸停、滑過、點(diǎn)擊更換圖片,因?yàn)镹SButton與UIButton不同,它沒有UIControlEvent這個(gè)枚舉,所以很多東西需要我們自己去寫。直接上代碼了
.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"]];
// 設(shè)置圖片鋪滿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 {
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
注意:我讓公司美工做圖的時(shí)候每個(gè)按鈕給了我三種狀態(tài),命名統(tǒng)一規(guī)范(不嫌麻煩的話也可以自己命名),_c代表按下狀態(tài)圖片,_m代表滑過時(shí)候的狀態(tài)圖片。 eg:圖片名稱為 “myImage”,按下圖片就為“myImage_c”,對(duì)應(yīng)滑過圖片就是 “myImage_m”,這樣所有需要三種狀態(tài)的按鈕都會(huì)繼承這個(gè)類。
其中上面代碼中有兩個(gè)我注釋掉的方法mouseDown、mouseUp,分別為按下、抬起,起初我是想用這兩個(gè)方法,改變button的背景圖片,但是問題來了,這兩個(gè)方法會(huì)和button的select方法沖突!!!這就蛋疼了,雖然可以用mouseDown代替select方法,但是就起不到封裝的目的了,如果想判斷這兩個(gè)方法的發(fā)起時(shí)間也有方法,但是實(shí)在是麻煩,最后找到了這個(gè)屬性alternateImage,雖然以前就知道,但是之前都不好使,原來它要搭配[self.cell setHighlightsBy:NSContentsCellMask];使用。
最后,因?yàn)楣疽蟮陌粹o比較統(tǒng)一,所以很多屬性我都是直接寫在了這個(gè)類里面,如果需要的同學(xué)可以單獨(dú)寫在外面賦值。
補(bǔ)充:代碼中有一個(gè)方法,是改變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?下載地址