取消隐藏光标滞后

Unhiding cursor lags

我有一个弹出窗口的一部分-window 我在其中绘制了一个带有线条的自定义光标。因此,我不希望在特定区域 (isInDiagram) 内显示标准光标。

这是我的代码:

- (void)mouseMoved:(NSEvent *)theEvent {
   position = [self convertPoint:[theEvent locationInWindow] fromView:nil];
   if(![self isInDiagram:position]) {
       [NSCursor unhide];
   }
   else{
       [NSCursor hide];
   }
   [self setNeedsDisplay: YES];
}

- (bool) isInDiagram: (NSPoint) p {
   return (p.x >= bborder.x + inset) && (p.y >= bborder.y + inset) &&
   (p.x <= self.window.frame.size.width - bborder.x - inset) &&
   (p.y <= self.window.frame.size.height - bborder.y - inset);
}

现在隐藏光标效果很好,但取消隐藏总是滞后。我不知道是什么最终触发了光标再次显示。但是,如果我循环取消隐藏命令取消隐藏工作:

for (int i = 0; i<100; i++) {
     [NSCursor unhide];
}

有什么办法可以在不使用这个难看的循环的情况下解决这个问题吗?

来自文档:

Each invocation of unhide must be balanced by an invocation of hide in order for the cursor display to be correct.

当你移动鼠标时它隐藏了多次。如果光标尚未隐藏,则需要标记而不是仅隐藏。它应该只隐藏一次。

- (void)mouseMoved:(NSEvent *)theEvent {
   position = [self convertPoint:[theEvent locationInWindow] fromView:nil];
   BOOL isInDiagram = [self isInDiagram:position]
   if(!isInDiagram && !CGCursorIsVisible()) {
       [NSCursor unhide];
   }
   else if (isInDiagram && CGCursorIsVisible()){ // cursor is not hidden
       [NSCursor hide];
   }
   [self setNeedsDisplay: YES];
}

注意CGCursorIsVisible 已弃用您可以维护自己的标志以跟踪光标隐藏状态。