首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在鼠标悬停时突出显示IKImageBrowserView的单元格?

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

Stack Overflow用户
提问于 2013-03-14 20:37:14
回答 2查看 347关注 0票数 5

当鼠标悬停在IKImageBrowserView的一个单元上时,我想提供一些反馈。

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-24 01:13:02

您可以尝试将IKImageBrowserView子类化并将NSTrackingArea添加到可见的rect中。然后,您可以使用-indexOfItemAtPoint:使用implement -mouseMoved: to work with the tracking area来定位正确的图像单元,然后使用-itemFrameAtIndex:获取它的框架。

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

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

票数 2
EN

Stack Overflow用户

发布于 2016-06-17 08:59:04

虽然这已经得到了回答(虽然没有代码),但我也遇到了同样的需求,我通过子类化IKImageBrowswerView实现了它。下面是我的代码,我希望它能帮助一些人。要使用它,只需将xib/nib/storyboard中的图像浏览器视图的类设置为CustomIKImageBrowserView,并将以下类添加到项目中。

代码语言:javascript
复制
     #import <Quartz/Quartz.h>
    #import "CustomizableNSView.h"
    @interface CustomIKImageBrowserView : IKImageBrowserView
    {
        NSTrackingArea *trackingArea;
        NSInteger lastHoverIndex;
        CustomizableNSView *hoverView ;
    }
    @end

实现类是:

代码语言:javascript
复制
#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是:

代码语言:javascript
复制
#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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15409502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档