5

当鼠标悬停在IKImageBrowserView.

具体来说,我想稍微调整一下单元格的大小,使其在鼠标悬停时显得稍大一些。或者,突出显示背景/边框就可以了。

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

4

2 回答 2

2

您可以尝试继承 IKImageBrowserView 并将 NSTrackingArea 添加可见矩形。然后,您可以实现 -mouseMoved: 与跟踪区域一起使用 -indexOfItemAtPoint: 定位正确的图像单元格,然后使用-itemFrameAtIndex :获取其帧。

与其尝试使用 IKImageBrowserView 的黑盒图层和绘图,不如在新发现的框架上方添加自己的图层(或无边框窗口)并使用标准动画来增长/缩小/动画/发光/摇晃/无论这个“作弊” “ 细胞。让点击“通过”(并隐藏您的“作弊”单元格),以便正常的拖放机制工作相同。IKImageBrowserView 有它自己的 -setForegroundLayer: 方法,它允许你添加一个“覆盖层”——我想非常适合这个目的。

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

于 2013-03-23T17:13:01.897 回答
0

虽然这已经得到回答(虽然没有代码),但我也遇到了同样的要求,我通过继承 IKImageBrowserView 来实现它。下面是我的代码,我希望它会对某人有所帮助。要使用它,只需将 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

我希望它可以帮助某人并加快开发速度。

于 2016-06-17T00:59:04.137 回答