核心情节mouseMoved

时间:2014-02-17 00:50:36

标签: objective-c macos cocoa core-plot

当鼠标移动/悬停在符号或线上时,我想在scatterplot中显示(注释)值。我之前已经问过这个问题,但我只能找到需要用户点击图表来显示此信息的答案。我试图在plotView委托中实现mouseMoved方法:

- (void)mouseMoved:(NSEvent *)theEvent
{
    NSPoint location = [hostingView convertPoint: [theEvent locationInWindow] fromView: nil];
    CGPoint mouseLocation = NSPointToCGPoint(location);
    CGPoint pointInHostedGraph = [hostingView.layer convertPoint: mouseLocation toLayer:  plotItem.graph.plotAreaFrame.plotArea];

    NSUInteger index = [self.plotItem.dataSourceLinePlot indexOfVisiblePointClosestToPlotAreaPoint: pointInHostedGraph];
    NSLog(@"test: %lu",(unsigned long)index);

}

这里,plotItem是NSObject的子类,在example中定义为PlotItem.h,托管视图是CPTGraphHostingView的一个实例。我还补充说:

    CPTGraphHostingView *hostedlayer=[self.plotItem updateView:hostingView height:hostingView.frame.size.height width:hostingView.frame.size.width];

    [self.plotItem renderInView:hostedlayer withTheme:theme animated:YES withData:self.myFlattenedNodes];

    hostedlayer.window.acceptsMouseMovedEvents = YES;
    [hostedlayer.window makeFirstResponder:hostingView];

但是我的mouseMoved方法没有被调用。我在这里做错了什么,因为我无法让mouseMoved响应我的悬停。我是否需要将NSTrackingArea添加到hostedLayer?建议表示赞赏。 Ť

更新和解决方案: 我遵循了Erics的建议,并将我的CPTGraphHostingView子类化,我实现了以下内容:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.window.acceptsMouseMovedEvents = YES;
        [self.window makeFirstResponder:self];

        area = [[NSTrackingArea alloc] initWithRect:self.frame
                                            options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow| NSTrackingActiveAlways)
                                              owner:self userInfo:nil];

        [self addTrackingArea:area];
        [self becomeFirstResponder];
    }
    return self;
}

- (void)updateTrackingAreas {
    [self removeTrackingArea:area];
    area = [[NSTrackingArea alloc] initWithRect:self.frame
                                    options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow| NSTrackingActiveAlways)
                                      owner:self userInfo:nil];
    [self addTrackingArea:area];
}

- (void)mouseMoved:(NSEvent *)theEvent
{
    NSPoint location = [self convertPoint: [theEvent locationInWindow] fromView: nil];
    CGPoint mouseLocation = NSPointToCGPoint(location);
    [self setLocationOfMouse:[self.layer convertPoint: mouseLocation toLayer:nil]];
}

-(void) dealloc{
    [self removeTrackingArea:area];
}

我还定义了类的属性:

CGPoint locationOfMouse;
NSTrackingArea *area;

在我的控制器中,我为locationOfMouse属性添加了一个观察者:

    [self.plotItem.graphHostingView addObserver:self
                        forKeyPath:@"locationOfMouse"
                           options:0
                           context:NULL];

触发了我绘制注释的方法:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
     if ( [keyPath isEqualToString:@"locationOfMouse"] ) {
        CGPoint location = self.plotItem.graphHostingView.locationOfMouse;
        [self.plotItem mouseMovedOverGraph:location];    
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object
                           change:change context:context];
    }
} 

1 个答案:

答案 0 :(得分:1)

Core Plot不会将鼠标移动的事件传递给任何代理。子类CPTGraphHostingView(它是NSView的子类)并在那里实现-mouseMoved:方法。 Core Plot仅使用鼠标按下,拖动和向上事件,因此您不会干扰任何内置事件处理。