NSView的mouseMoved事件

时间:2012-04-08 18:51:06

标签: objective-c xcode cocoa

在我的app委托中,我创建了一个窗口“helpWindow”,并将其内容视图设置为NSView子类。在我的子类中,我使用drawRect并确保它是关键窗口。我遇到的问题是,在我的鼠标事件上,鼠标按下事件与内容视图一起工作正常但是,鼠标移动不起作用并显示位置。我是否必须向mouseLocation添加内容?我觉得drawRect掩盖了鼠标移动事件。谢谢!

//在我的appDelegate.m

controlFilterBox = [[MoveFilter alloc] initWithFrame:helpWindow.frame];
[helpWindow setContentView:controlFilterBox];
[controlFilterBox release];

//在我的NSView subclass.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);
      }

1 个答案:

答案 0 :(得分:2)

我找到了答案,事实证明最好创建一个NSTrackingArea,以便能够在NSView中使用mouseMoved事件。