Mimo, że jest już odpowiedział (bez kodu), ale ja również natknąłem się na to samo wymaganie i wdrożyłem go przez podklasy IKImageBrowswerView. Poniżej znajduje się mój kod i mam nadzieję, że komuś to pomoże. Aby z niego skorzystać wystarczy ustawić klasę widoku przeglądarki obrazu w Xib/nib/storyboard na CustomIKImageBrowserView i dodać poniższe klasy do projektu.
#import <Quartz/Quartz.h>
#import "CustomizableNSView.h"
@interface CustomIKImageBrowserView : IKImageBrowserView
{
NSTrackingArea *trackingArea;
NSInteger lastHoverIndex;
CustomizableNSView *hoverView ;
}
@end
I klasa wykonawczych jest:
#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
A CustomizableNSView jest:
#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
Mam nadzieję, że komuś pomóc i przyspieszyć rozwój.
Jak dodać NSTrackingArea do IKImageBrowserView, jeśli metoda addTrackingArea: jest niedostępna (nie jest to widok)? – aneuryzm
Błąd ... IKImageBrowserView * to * widok. W nazwie ma nawet słowo "Widok". Dziedziczy bezpośrednio * od * NSView, w rzeczywistości tak -addTrackingArea: jest dostępna; po prostu nie jest w referencji IKImageBrowserView (ponieważ jest w referencji NSView). :-) Ty ** zdecydowanie ** musisz poświęcić więcej czasu na uczenie się, jak przejść przez odniesienie do API jak ninja, ponieważ jego dziedzictwo (i link do NSView) znajduje się na samej górze jego strony referencyjnej. –