11

I have an NSCollectionView with a few NSViews in it. The NSView has an NSBox in it that changes color when it is selected. I want to also make the NSBox change color when hovered over.

I subclassed NSBox and added the mouseEntered and mouseExited methods. I used addTrackingRect inside of viewWillMoveToWindow but the problem is that the hover over effect only happens if I first select the subview that the box is in.

Furthermore only the box that is selected has the Hover Over effect happening on it. How can I implement the Hover Over effect such that all the NSViews in my NSCollectionView show the effect immediately?

marcopolo
  • 1,963
  • 4
  • 18
  • 31

2 Answers2

5

You can override updateTrackingAreas in a subclass of NSView to accomplish this behavior:

Interface

@interface HoverView : NSView

@property (strong, nonatomic) NSColor *hoverColor;

@end

Implementation

@interface HoverView ()

@property (strong, nonatomic) NSTrackingArea *trackingArea;
@property (assign, nonatomic) BOOL mouseInside;

@end

@implementation HoverView

- (void) drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    // Draw a white/alpha gradient
    if (self.mouseInside) {
        [_hoverColor set];
        NSRectFill(self.bounds);
    }
}


- (void) updateTrackingAreas {
    [super updateTrackingAreas];

    [self ensureTrackingArea];
    if (![[self trackingAreas] containsObject:_trackingArea]) {
        [self addTrackingArea:_trackingArea];
    }
}

- (void) ensureTrackingArea {
    if (_trackingArea == nil) {
        self.trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
                                                         options:NSTrackingInVisibleRect | NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited
                                                           owner:self
                                                        userInfo:nil];
    }
}

- (void) mouseEntered:(NSEvent *)theEvent {
    self.mouseInside = YES;
}

- (void) mouseExited:(NSEvent *)theEvent {
    self.mouseInside = NO;
}

- (void) setMouseInside:(BOOL)value {
    if (_mouseInside != value) {
        _mouseInside = value;
        [self setNeedsDisplay:YES];
    }
}


@end
Sheamus
  • 6,506
  • 3
  • 35
  • 61
0

Swift 5

    
    var mouseInside : Bool = false { didSet {
        needsDisplay = true
    }}
    @IBInspectable var hoverColor : NSColor = .controlAccentColor
    var trackingArea : NSTrackingArea?

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        
        if mouseInside {
            hoverColor.setFill()
            bounds.fill()
        }
    }
    
    override func updateTrackingAreas() {
        super.updateTrackingAreas()
        
        ensureTrackingArea()
        
        if let trackingArea = trackingArea, !trackingAreas.contains(trackingArea) {
            addTrackingArea(trackingArea)
        }
    }
    
    func ensureTrackingArea() {
        if trackingArea == nil {
            trackingArea = NSTrackingArea(rect: .zero,
                                          options: [
                                            .inVisibleRect,
                                            .activeAlways,
                                            .mouseEnteredAndExited],
                                          owner: self,
                                          userInfo: nil)
        }
    }
    
    override func mouseEntered(with event: NSEvent) {
        mouseInside = true
    }
    
    override func mouseExited(with event: NSEvent) {
        mouseInside = false
    }
    
    
    
}
Morten J
  • 814
  • 10
  • 21