objective-c - 核心情节 mouseMoved

标签 objective-c macos cocoa core-plot

我想在 scatterplot 中显示(注释)值当鼠标移动/悬停在符号或线条上时。我知道之前有人问过这个问题,但我只能找到要求用户单击绘图以显示此信息的答案。我试图在我的 plotView 委托(delegate)中实现 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 一样定义。托管 View 是 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;

在我的 Controller 中,我为 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];
    }
} 

最佳答案

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

关于objective-c - 核心情节 mouseMoved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21818964/

相关文章:

objective-c - 像 Xcode 中一样实现 New-File-dialog

objective-c - 子类化NSWindowController,我可以提供多个init方法吗?

python - 如何在 iTerm 2(使用 API)中获取和设置窗口、选项卡和 session 的标题?

iphone - 选项卡栏 Controller + 导航 Controller + 栏按钮项目

swift - 在拖动过程中防止 NSCollectionView 'lifting' 项目

iphone - bundle 标识符与先前的 bundle 标识符不同

ios - 设置自定义 UITableView tableHeaderView

php - 通过 SSH 将 MySQL 绑定(bind)到本地端口 - 在控制台中工作,而不是通过 Mac OSX 中的 PHP shell_exec()

objective-c - Cocoa 中的绘图选择框(橡皮筋、行进的 Ant ),Objective C

iphone - 寻找 Iphone 教程应用程序来搜索我所在位置附近的 field