Cocoa nsview更改游标

时间:2015-04-28 06:34:50

标签: objective-c macos cocoa nscursor

我尝试更改cocoa应用程序中的默认光标。我读到了这个,但标准方法对我不起作用。

我尝试将此方法添加到我的OpenGLView子类中:

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

} 

它不起作用。为什么呢?

1 个答案:

答案 0 :(得分:10)

有两种方法可以做到。首先 - 最简单的 - 是在鼠标进入视图并离开时更改光标。

- (void)mouseEntered:(NSEvent *)event
  {
   [super mouseEntered:event];
   [[NSCursor pointingHandCursor] set];
  }

- (void)mouseExited:(NSEvent *)event
  {
   [super mouseExited:event];
   [[NSCursor arrowCursor] set];
  }

另一种方法是创建跟踪区域(即awakeFromNib - 方法),并覆盖- (void)cursorUpdate: - 方法

- (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];
  }