我在一个班级中有一个NSTableView
。
我在TrackingArea
类中实现了mouseEntered
,mouseExited
,drawBackgroundInRect
,drawSelectionInRect
,HoverTableCellView
,这是NSTableRowView
的子类。
在xib文件中,我已经NSView
并在身份检查器的类字段中将其重命名为HoverTableCellView
,并将其放在NSTableView
的第一行。
当我把鼠标放在它上面时,单独的第一行工作正常。
我的整个表格不像第一行那样。
- (void)setMouseInside:(BOOL)value {
if (mouseInside != value) {
mouseInside = value;
[self setNeedsDisplay:YES];
}
}
- (BOOL)mouseInside {
return mouseInside;
}
- (void)ensureTrackingArea {
if (trackingArea == nil) {
trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:NSTrackingInVisibleRect | NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
}
}
- (void)updateTrackingAreas {
[super updateTrackingAreas];
[self ensureTrackingArea];
if (![[self trackingAreas] containsObject:trackingArea]) {
[self addTrackingArea:trackingArea];
}
}
- (void)mouseEntered:(NSEvent *)theEvent {
self.mouseInside = YES;
}
- (void)mouseExited:(NSEvent *)theEvent {
self.mouseInside = NO;
}
static NSGradient *gradientWithTargetColor(NSColor *targetColor) {
NSArray *colors = [NSArray arrayWithObjects:[targetColor colorWithAlphaComponent:0], targetColor, targetColor, [targetColor colorWithAlphaComponent:0], nil];
const CGFloat locations[4] = { 0.0, 0.35, 0.65, 1.0 };
return [[NSGradient alloc] initWithColors:colors atLocations:locations colorSpace: [NSColorSpace sRGBColorSpace]];
}
- (void)drawBackgroundInRect:(NSRect)dirtyRect {
// Custom background drawing. We don't call super at all.
[self.backgroundColor set];
// Fill with the background color first
NSRectFill(self.bounds);
// Draw a white/alpha gradient
if (self.mouseInside) {
NSGradient *gradient = gradientWithTargetColor([NSColor whiteColor]);
[gradient drawInRect:self.bounds angle:0];
}
}
//仅在'selected'属性为yes时调用。 - (void)drawSelectionInRect:(NSRect)dirtyRect {
// Check the selectionHighlightStyle, in case it was set to None
if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) {
// We want a hard-crisp stroke, and stroking 1 pixel will border half on one side and half on another, so we offset by the 0.5 to handle this
NSRect selectionRect = NSInsetRect(self.bounds, 5.5, 5.5);
[[NSColor colorWithCalibratedWhite:.72 alpha:1.0] setStroke];
[[NSColor colorWithCalibratedWhite:.82 alpha:1.0] setFill];
NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect xRadius:10 yRadius:10];
//[selectionPath fill];
[selectionPath stroke];
NSLog(@"asasasa");
}
}
- (NSBackgroundStyle)interiorBackgroundStyle {
return NSBackgroundStyleLight;
}
- (void)setFrame:(NSRect)frameRect {
[super setFrame:frameRect];
if ([self inLiveResize]) {
if (self.selected|| self.mouseInside) {
[self setNeedsDisplay:YES];
} else {
}
}
}