- (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]; }}解决方法 Core Plot不会将鼠标移动的事件传递给任何代理.子类CPTGraphHostingVIEw(它是NSVIEw的子类)并在那里实现-mouseMoved:方法. Core Plot仅使用鼠标按下,拖动和向上事件,因此您不会干扰任何内置事件处理. 总结
以上是内存溢出为你收集整理的objective-c – 核心情节mouseMoved全部内容,希望文章能够帮你解决objective-c – 核心情节mouseMoved所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)