0

我有一个弹出窗口的一部分,我用线条绘制了一个自定义光标。因此,我不希望标准光标显示在某个区域(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];
}

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

4

1 回答 1

2

从文档:

每次调用 unhide 都必须通过调用 hide 来平衡,以使光标显示正确。

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

- (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不推荐使用您可以维护自己的标志来跟踪光标隐藏状态。

于 2017-04-15T13:04:14.643 回答