( ), , 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];
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{
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];
[self.borderColor set];
[bgPath stroke];
if(self.backgroundColor){
[self.backgroundColor set];
[bgPath fill];
}
}
}
@end
, - .