我想提供一些反饋,當鼠標在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