How can I select IKImageBrowserView cells above the mouse?

I would like to provide some feedback when the mouse ended up in one of the cells IKImageBrowserView.

In particular, I would like to resize the cell a bit, so that it seems a little larger on hover. Alternatively, highlighting the background / border would be great.

Unfortunately, IKImageBrowserCellit is not a subclass NSCell, but rather NSObject, and I can not find a solution in the API. Any ideas?

+5
source share
2 answers

IKImageBrowserView NSTrackingArea . -mouseMoved: , , - indexOfItemAtPoint:, , - itemFrameAtIndex:.

, IKImageBrowserView, ( ) ///// "-". "" ( "" ), . , IKImageBrowserView -setForegroundLayer: , " overlay" - .

, .

+2

( ), , IKImageBrowswerView. , , -. , xib/nib/storyboard CustomIKImageBrowserView .

     #import <Quartz/Quartz.h>
    #import "CustomizableNSView.h"
    @interface CustomIKImageBrowserView : IKImageBrowserView
    {
        NSTrackingArea *trackingArea;
        NSInteger lastHoverIndex;
        CustomizableNSView *hoverView ;
    }
    @end

:

#import "CustomIKImageBrowserView.h"

    @implementation CustomIKImageBrowserView

- (void)awakeFromNib {
    [self addCustomTrackingAreaToChangeMouseCursor];
}

- (void) updateTrackingAreas {
    if (trackingArea)
        [self removeTrackingArea:trackingArea];
    [self addCustomTrackingAreaToChangeMouseCursor];
}

- (void) addCustomTrackingAreaToChangeMouseCursor{
    trackingArea = [[NSTrackingArea alloc]  initWithRect:self.bounds options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingMouseMoved owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void) mouseMoved:(NSEvent *)theEvent{
    NSPoint currentPosition = [self convertPoint:[theEvent locationInWindow] fromView:nil];

    NSInteger idx  = [self indexOfItemAtPoint:currentPosition];
    if(idx != NSNotFound) {
        [[NSCursor pointingHandCursor] push];
        //NSLog(@"DslrIKImageBrowserView = %ld and %ld",idx,lastHoverIndex);
        if (lastHoverIndex == idx) {
            return;
        } else {
            if(hoverView)
                [hoverView removeFromSuperview];
        }

        lastHoverIndex = idx;
        IKImageBrowserCell *cell = [self cellForItemAtIndex:idx];
        NSRect r = cell.imageFrame;
        r.size.width = r.size.width + 6;
        r.size.height = r.size.height + 6;
        r.origin.x = r.origin.x - 3;
        r.origin.y = r.origin.y - 3 ;

        hoverView = [[CustomizableNSView alloc] initWithFrame:r];
        hoverView.borderColor   = [NSColor colorWithCalibratedRed:136/255.0 green:185/255.0 blue:236/255.0 alpha:1.0];
        hoverView.borderRadious = 0;
        hoverView.borderWidth   = 6;
        hoverView.backgroundColor = [NSColor colorWithCalibratedRed:0 green:191.0/255.0 blue:1.0 alpha:0.3];
        [self.superview addSubview:hoverView];

    } else
    {
        lastHoverIndex = -1;
        [[NSCursor arrowCursor] push];
        if(hoverView)
            [hoverView removeFromSuperview];
    }
}

- (void)mouseEntered:(NSEvent *)theEvent{
    [[NSCursor pointingHandCursor] push];
}

- (void) mouseExited:(NSEvent *)theEvent{
    //[[NSCursor arrowCursor] push];
    lastHoverIndex = -1;
    if(hoverView) [hoverView removeFromSuperview];
} @end

CustomizableNSView:

#import <Cocoa/Cocoa.h>

@interface CustomizableNSView : NSView
{

}
@property (nonatomic) NSRect boundsToCustomize;
@property (nonatomic) CGFloat borderWidth;
@property (nonatomic) CGFloat borderRadious;
@property (nonatomic) NSColor *borderColor;
@property (nonatomic) NSColor *backgroundColor;


@end
=================
#import "CustomizableNSView.h"

@implementation CustomizableNSView


- (void)drawRect:(NSRect)dirtyRect {

    [super drawRect:dirtyRect];
    NSRect r = self.bounds;

    if(!NSIsEmptyRect(self.boundsToCustomize))
        r = self.boundsToCustomize;
    if(self.borderColor){
        NSBezierPath * bgPath = [NSBezierPath bezierPathWithRoundedRect: r xRadius: self.borderRadious yRadius: self.borderRadious];
        bgPath.lineWidth = self.borderWidth;
        NSAffineTransform * t = [NSAffineTransform transform];
        [t translateXBy: 0.5 yBy: 0.5];
        [bgPath transformUsingAffineTransform: t];
        //NSColor* rgbColor = [NSColor colorWithCalibratedRed:101.0/255.0 green: 101.0/255.0  blue:101.0/255.0  alpha:0.5];
        [self.borderColor set];
        [bgPath stroke];

        if(self.backgroundColor){
            [self.backgroundColor set];
            [bgPath fill];
        }
    }


}
@end

, - .

0

All Articles