我想提供一些反馈,当鼠标在IKImageBrowserView
的某个单元上结束时。如何在鼠标上突出显示IKImageBrowserView的单元格?
具体而言,我想稍微调整单元格大小,使其在鼠标悬停时显示稍大。或者,突出显示背景/边框将会很好。
不幸的是,IKImageBrowserCell
不是NSCell
的子类,而是NSObject
,我无法在API中找到解决方案。有任何想法吗?
我想提供一些反馈,当鼠标在IKImageBrowserView
的某个单元上结束时。如何在鼠标上突出显示IKImageBrowserView的单元格?
具体而言,我想稍微调整单元格大小,使其在鼠标悬停时显示稍大。或者,突出显示背景/边框将会很好。
不幸的是,IKImageBrowserCell
不是NSCell
的子类,而是NSObject
,我无法在API中找到解决方案。有任何想法吗?
您可以尝试继承IKImageBrowserView并将NSTrackingArea添加到可见矩形。您可以使用-indexOfItemAtPoint:然后implement -mouseMoved: to work with the tracking area找到正确的图像单元,然后通过使用-itemFrameAtIndex:来获取其框架。您可以在新发现的框架上添加您自己的图层(或无边界窗口),并使用标准动画来增长/缩小/动画/发光/摇动/缩放图像,而不是尝试使用IKImageBrowserView的黑盒图层和绘图。无论这个“欺骗”细胞。让点击“通过”(并隐藏你的“欺骗”单元格),这样正常的拖放机制的作用是一样的。 IKImageBrowserView有它自己的-setForegroundLayer:方法,允许你添加一个“覆盖层” - 完全适合这个目的,我想象。
这是我第一次尝试解决这个问题,如果它是我自己的。
虽然这已经被回答(虽然没有代码),但我也遇到了相同的要求,我通过继承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
我希望这会帮助别人的同时将加快发展。
如果addTrackingArea:方法不可用(它不是视图),如何将NSTrackingArea添加到IKImageBrowserView? – aneuryzm 2013-03-23 17:52:45
错误... IKImageBrowserView *是*一个视图。它的名字甚至有“View”一词。它直接从* NSView继承*,实际上,所以-addTrackingArea:可用;它只是不在IKImageBrowserView引用中(因为它在NSView引用中)。 :-)你肯定**需要花更多的时间学习如何像忍者那样遍历API参考,因为它的继承(和NSView的链接)正好位于其参考页面的顶部。 – 2013-03-28 19:23:36