我想在 a 中显示(注释)值散点图 https://code.google.com/p/core-plot/source/browse/examples/CorePlotGallery/src/plots/SimpleScatterPlot.m?r=abfc0cb289b890ed0fbbf8ee856ec852d73796a3当鼠标移动/悬停在符号或线条上时。我知道这个问题之前已经被问过,但我只能找到需要用户单击绘图来显示此信息的答案。我尝试在我的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的子类,其定义类似于PlotItem.h中的example https://code.google.com/p/core-plot/source/browse/examples/CorePlotGallery/src/shared/PlotItem.h?r=5e338f062b6dae3d339e5d286f1011705b705510托管视图是 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 响应我的悬停。我需要向 HostedLayer 添加 NSTrackingArea 吗?欢迎提出建议。时间
更新及解决方案:我遵循了 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];
}
}