在我的应用程序委托(delegate)中,我创建了一个窗口“helpWindow”,并将其内容 View 设置为 NSView 子类。在我的子类中,我绘制矩形并确保它是关键窗口。我遇到的问题是,在我的鼠标事件中,鼠标按下事件在内容 View 中工作正常,但是,移动的鼠标不起作用并显示位置。我是否必须在 mouseLocation
中添加一些内容?我觉得drawRect掩盖了鼠标移动事件。谢谢!
//在我的appDelegate.m
controlFilterBox = [[MoveFilter alloc] initWithFrame:helpWindow.frame];
[helpWindow setContentView:controlFilterBox];
[controlFilterBox release];
//在我的 NSView 子类中。m
-(void)drawRect:(NSRect)dirtyRect
{
[[NSColor redColor] set];
NSRectFill(dirtyRect);
[[self window] makeKeyWindow];
}
-(void)mouseDown:(NSEvent *)theEvent
{
NSPoint eventLocation = [theEvent locationInWindow];
NSPoint location = [self convertPoint:eventLocation fromView:nil];
NSLog(@"exit %f %f", location.x, location.y);
}
-(void)mouseMoved:(NSEvent *)theEvent
{
NSPoint mouseLoc = [NSEvent mouseLocation];
NSLog(@"mouseMoved: %f %f", mouseLoc.x, mouseLoc.y);
}
最佳答案
我找到了答案,事实证明最好创建一个 NSTrackingArea 以便能够在 NSView 中使用 mouseMoved 事件。
关于objective-c - NSView 的 mouseMoved 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10065442/