核心情节mouseMoved [英] Core plot mouseMoved

查看:112
本文介绍了核心情节mouseMoved的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在

I would like to show (annotate) values in a scatterplot as the mouse is moved/hovering over a symbol or line. I understand this question has been asked before but I can only find answers that require the user to click the plot to show this information. I have tried to implement the mouseMoved method in my plotView delegate:

- (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的子类,并且在

Here, plotItem is a subclass of NSObject and defined like PlotItem.h in the example and hosting view is an instance of CPTGraphHostingView. I also add:

    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吗?建议表示赞赏. T

But my mouseMoved method does not get called. What am I doing wrong here as I am unable to get mouseMoved to respond to my hovering. do I need to add a NSTrackingArea to hostedLayer ? Suggestions are appreciated. T

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

Update and solution: I followed Erics' suggestion and subclassed my CPTGraphHostingView where I implemented the following:

- (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];
}

我也定义了该类的属性:

I also defined to properties of the class:

CGPoint locationOfMouse;
NSTrackingArea *area;

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

In my controller I added an observer for the locationOfMouse property:

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

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

Which triggered my method that draws the annotation:

-(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];
    }
} 

推荐答案

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

Core Plot doesn't pass mouse moved events to any of its delegates. Subclass CPTGraphHostingView (which is a subclass of NSView) and implement the -mouseMoved: method there. Core Plot only uses the mouse down, dragged, and up events so you won't interfere with any of the built-in event handling.

这篇关于核心情节mouseMoved的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆