Objective c NSView的mouseMoved事件
在我的AppDelegate中,我创建了一个窗口“helpWindow”,并将其内容视图设置为NSView子类。在我的子类中,我drawRect并确保它是键窗口。我遇到的问题是,在我的鼠标事件中,鼠标按下事件与内容视图配合良好,但是,移动的鼠标不起作用并显示位置。我是否必须向Objective c NSView的mouseMoved事件,objective-c,xcode,cocoa,Objective C,Xcode,Cocoa,在我的AppDelegate中,我创建了一个窗口“helpWindow”,并将其内容视图设置为NSView子类。在我的子类中,我drawRect并确保它是键窗口。我遇到的问题是,在我的鼠标事件中,鼠标按下事件与内容视图配合良好,但是,移动的鼠标不起作用并显示位置。我是否必须向鼠标位置添加内容?我觉得drawRect正在掩盖鼠标移动事件。谢谢 //在我的appDelegate.m中 controlFilterBox = [[MoveFilter alloc] initWithFrame:helpW
鼠标位置添加内容?我觉得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事件