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

5
当鼠标悬停在 IKImageBrowserView 中的一个单元格上时,我想提供一些反馈。具体来说,我想将单元格稍微调整大小,使其在鼠标悬停时略微变大。另外,高亮显示背景/边框也可以。
不幸的是,IKImageBrowserCell 不是 NSCell 的子类,而是 NSObject,我找不到 API 中的解决方案。有什么想法吗?
2个回答

2
你可以尝试子类化 IKImageBrowserView,并将 NSTrackingArea 添加到可见的矩形区域。然后,你可以 实现 -mouseMoved: 与 tracking area 配合工作,使用 -indexOfItemAtPoint: 找到正确的图像单元,然后通过 -itemFrameAtIndex: 来获取它的框架。
与其尝试修改IKImageBrowserView的黑盒子层和绘图,不如在新发现的框架上方添加自己的层(或无边框窗口),并使用标准动画来增长/缩小/动画/发光/摇晃/等等这个“作弊”单元格。让点击“穿过”(并隐藏您的“作弊”单元格),以便正常的拖放机制能够正常工作。 IKImageBrowserView具有自己的-setForegroundLayer:方法,允许您添加“覆盖层” - 非常适合此目的,我想象。

如果这是我的问题,那将是我解决这个问题的第一步尝试。


如果addTrackingArea: 方法不可用(因为它不是一个视图),我该如何向IKImageBrowserView添加NSTrackingArea - aneuryzm
Err... IKImageBrowserView 一个视图。它的名字中甚至有“View”这个词。实际上,它直接从NSView继承,因此-addTrackingArea:可用;只是在IKImageBrowserView参考文献中没有(因为它在NSView参考文献中)。 :-) 您绝对需要花更多时间学习如何像忍者一样遍历API参考,因为它的继承关系(以及指向NSView的链接)就在其参考页面的顶部。 - Joshua Nozzi

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

可自定义的NSView是:

#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

我希望它能帮助到某人,并加速开发。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接