问题描述
我已经为此工作了几个小时,不知道出了什么问题。我想为作为NSTextView子视图的按钮定制光标,我添加了一个跟踪区域,并在鼠标输入按钮时发送CursorUpdate消息。
每次鼠标进入跟踪区域时都会调用cursorUpdate方法。但是光标保持在IBeamCursor上。
有什么想法吗?
苹果文档参考:managing cursor-update event
- (void)cursorUpdate:(NSEvent *)event {
[[NSCursor arrowCursor] set];
}
- (void)myAddTrackingArea {
[self myRemoveTrackingArea];
NSTrackingAreaOptions trackingOptions = NSTrackingCursorUpdate | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
_trackingArea = [[NSTrackingArea alloc] initWithRect: [self bounds] options: trackingOptions owner: self userInfo: nil];
[self addTrackingArea: _trackingArea];
}
- (void)myRemoveTrackingArea {
if (_trackingArea)
{
[self removeTrackingArea: _trackingArea];
_trackingArea = nil;
}
}
推荐答案
我遇到了同样的问题。
问题是,NSTextView
每次收到mouseMoved:
事件时都会更新其游标。该事件由NSTextView
的自我更新NSTrackingArea
触发,该事件始终跟踪NSTextView
在NSScrollView
中的可见部分。所以我可能有两个解决方案。
覆盖updateTrackingAreas
删除Cocoa提供的跟踪区域,并确保您始终创建一个新区域,而不是该按钮。(我不会这么做的!)
覆盖mouseMoved:
,并确保当光标位于按钮上时不会调用SUPERR。
- (void)mouseMoved:(NSEvent *)theEvent {
NSPoint windowPt = [theEvent locationInWindow];
NSPoint superViewPt = [[self superview]
convertPoint: windowPt fromView: nil];
if ([self hitTest: superViewPt] == self) {
[super mouseMoved:theEvent];
}
}