Cocoa nsview change cursor

I tried to change the default cursor in my cocoa application. I read about this, but the standard approach does not work for me.

I am trying to add this method to my OpenGLView subclass:

- (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!");

} 

This does not work. Why?

+4
source share
1 answer

There are two ways to do this. The first - the easiest - is to change the cursor when 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 a tracking zone (i.e. in the awakeFromNib-method) and override the - (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];
  }
+7
source

Source: https://habr.com/ru/post/1585034/


All Articles