我有一个弹出窗口的一部分,我在其中绘制了带有线条的自定义光标。因此,我不希望标准光标显示在特定区域(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
已弃用,您可以维护自己的标志来跟踪光标隐藏状态。
关于objective-c - 取消隐藏光标滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43426254/