问题
I tried to change the default cursor in my cocoa application. I read about this but the standard approach isn't working for me.
I try to add to my OpenGLView subclass this method:
- (void) resetCursorRects
{
[super resetCursorRects];
NSCursor * myCur = [[NSCursor alloc] initWithImage:[NSImage imageNamed:@"1.png"] hotSpot:NSMakePoint(8,0)];
[self addCursorRect: [self bounds]
cursor: myCur];
NSLog(@"Reset cursor rect!");
}
It`s not working. Why?
回答1:
There're two ways you can do it. First - the most simple - is to change the cursor while the mouse enters the view and leaves it.
- (void)mouseEntered:(NSEvent *)event
{
[super mouseEntered:event];
[[NSCursor pointingHandCursor] set];
}
- (void)mouseExited:(NSEvent *)event
{
[super mouseExited:event];
[[NSCursor arrowCursor] set];
}
Another way is to create tracking area (i.e. in awakeFromNib
-method), and override - (void)cursorUpdate:
-method
- (void)createTrackingArea
{
NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingCursorUpdate;
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:self.bounds options:options owner:self userInfo:nil];
[self addTrackingArea:area];
}
- (void)cursorUpdate:(NSEvent *)event
{
[[NSCursor pointingHandCursor] set];
}
来源:https://stackoverflow.com/questions/29911989/cocoa-nsview-change-cursor