與其使用CGDisplayHideCursor和其神祕的不可讀隱藏計數來解決問題,解決方法是使用透明光標設置覆蓋整個窗口的光標矩形。這非常強大 - 當鼠標在窗口內並在其他時間顯示時,它可靠地隱藏光標。
我最終通過查看簡單DirectMedia Layer(SDL)2源代碼來了解這一點 - 這裏提供了一個最小的示例。
覆蓋resetCursorRects在您的NSView子類實現:
static NSCursor* invisibleCursor()
{
static NSCursor *invisibleCursor = NULL;
if (!invisibleCursor) {
/* RAW 16x16 transparent GIF */
static unsigned char cursorBytes[] = {
0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04,
0x01, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x10,
0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x8C, 0x8F, 0xA9, 0xCB, 0xED,
0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B
};
NSData *cursorData = [NSData dataWithBytesNoCopy:&cursorBytes[0]
length:sizeof(cursorBytes)
freeWhenDone:NO];
NSImage *cursorImage = [[[NSImage alloc] initWithData:cursorData] autorelease];
invisibleCursor = [[NSCursor alloc] initWithImage:cursorImage
hotSpot:NSZeroPoint];
}
return invisibleCursor;
}
- (void)resetCursorRects
{
[super resetCursorRects];
[self addCursorRect:[self bounds] cursor:invisibleCursor()];
}