7

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

我尝试将此方法添加到我的 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!");

} 

它不工作。为什么?

4

2 回答 2

15

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

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

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

另一种方法是创建跟踪区域(即在awakeFromNib-method 中),并覆盖- (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];
  }
于 2015-04-28T08:11:27.927 回答
3

对于那些正在寻找 Swift 解决方案的人来说,语法是:

override func mouseEntered(with event: NSEvent) {
    super.mouseEntered(with: event)

    NSCursor.pointingHand.set()
}
于 2020-05-08T16:09:55.890 回答