mac开发-----鼠标悬停事件(NSButton,NSImageView等)

很简单

#import"mouseView.h"

@implementation mouseView

- (id)initWithFrame:(NSRect)frame {

self = [super initWithFrame:frame];

if (self) {

// Initialization code here.

}

return self;

}

- (void)drawRect:(NSRect)dirtyRect {

NSLog(@"have draw");

if (flag==0) {

[[NSColor redColor] set]; //设置颜色

NSRectFill(dirtyRect);

} else {

[[NSColor yellowColor] set]; //设置颜色

NSRectFill(dirtyRect);

flag=0;

}

//填充rect区域.

}

- (void)viewDidMoveToWindow {

[self addTrackingRect:[self bounds] owner:self userData:nilassumeInside:NO];

}

//[cursor set] make the current cursor change

- (void)mouseEntered:(NSEvent *)theEvent

{

flag=1;

NSCursor *cursor = [NSCursor pointingHandCursor];

[cursor set];

[selfsetNeedsDisplay:YES];

}

//[cursor set] make the current cursor change

- (void)mouseExited:(NSEvent *)theEvent {

flag=0;

NSCursor *cursor = [NSCursor arrowCursor];

[cursor set];

[self setNeedsDisplay:YES];

}

@end

你可能感兴趣的:(mac开发-----鼠标悬停事件(NSButton,NSImageView等))