gpt4 book ai didi

objective-c - 核心情节 mouseMoved

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:40 26 4
gpt4 key购买 nike

我想在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com