如何在鼠标悬停时突出显示IKImageBrowserView的单元格?

时间:2013-03-14 12:37:14

标签: objective-c macos cocoa

我想在IKImageBrowserView的一个单元格上鼠标结束时提供一些反馈。

具体来说,我想稍微调整一下细胞大小,使其在鼠标悬停时显得略大一些。或者,突出显示背景/边框也没关系。

不幸的是,IKImageBrowserCell不是NSCell的子类,而是NSObject,我无法在API中找到解决方案。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您可以尝试继承IKImageBrowserView并将NSTrackingArea添加到可见的rect。然后,您可以implement -mouseMoved: to work with the tracking area使用-indexOfItemAtPoint:找到正确的图像单元格,然后使用-itemFrameAtIndex:获取其帧。

您可以在新发现的帧上方添加自己的图层(或无边框窗口),并使用标准动画来增长/缩小/动画/发光/摇动/不管,而不是尝试使用IKImageBrowserView的黑框图层和绘图。这个“骗子”细胞。让点击“通过”(并隐藏你的“作弊”单元格),这样普通的拖放机制就可以了。 IKImageBrowserView有自己的-setForegroundLayer:方法,允许你添加一个“覆盖层” - 非常适合这个目的,我想。

如果这是我自己的话,这是我第一次尝试解决这个问题。

答案 1 :(得分:0)

虽然已经回答了这个问题(虽然没有代码),但我也遇到了相同的要求,我通过继承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

我希望它会帮助某人并加快发展。