1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#import "BDRowView.h"
#define k_NORMAL_COLOR [NSColor colorFromInt:0xfcfdfe]
#define k_SELECTED_COLOR [NSColor colorFromInt:0xeff1f3]
@interface BDRowView ()
@property(strong) NSTrackingArea *trackingArea;
@property(assign) BOOL isHovering;
@end
@implementation BDRowView
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
}
- (void)drawSelectionInRect:(NSRect)dirtyRect {
if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) {
NSRect selectionRect = NSInsetRect(self.bounds, 0, 0);
[k_SELECTED_COLOR setStroke];
[k_SELECTED_COLOR setFill];
NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect xRadius:0 yRadius:0];
[selectionPath fill];
[selectionPath stroke];
}
}
-(void)updateTrackingAreas
{
if(self.trackingArea != nil) {
[self removeTrackingArea:self.trackingArea];
}
int opts = (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways);
self.trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
options:opts
owner:self
userInfo:nil];
[self addTrackingArea:self.trackingArea];
}
- (void)mouseEntered:(NSEvent *)theEvent {
self.isHovering = YES;
[self setBackgroundColor:[self getBackgroundColor:YES]];
}
- (void)mouseExited:(NSEvent *)theEvent {
self.isHovering = NO;
[self setBackgroundColor:[self getBackgroundColor:NO]];
}
-(NSColor*)getBackgroundColor:(BOOL)isSelected
{
if(isSelected) {
return k_SELECTED_COLOR;
}else{
return k_NORMAL_COLOR;
}
}
@end
自定义NSTableRowView实现鼠标跟踪动态显示选中/非选中颜色
该博客文章由作者通过 CC BY 4.0 进行授权。