我在x轴上使用日期标签,并希望使用自定义视图高亮显示特定日期。我打算用axis:labelWasSelected:
,移动annotationFrame到标签的位置和滚动它时,轴范围变化(呈三角core plot Framework - How to set custom UIView in CPTPlotSpaceAnnotation)(移动在plotSpacewillChangePlotRangeToForCoordinate:
注释)Coreplot - 轴标签的坐标和大小
我怎样才能获得标签的坐标从plotSpacewillChangePlotRangeToForCoordinate调用时?
编辑: 我已经成功实施移动标注由用户选择调用:我保存_selectedLabel
和发送newFrame的时候的情节变化范围。在2个问题,这:变化范围在注释框架的
延迟 - 看的电影:https://www.youtube.com/watch?v=R66ew6GAiJU&feature=youtu.be
当注释标签离开屏幕时,
_selectedLabel
被“遗忘”。当移回屏幕时,可能会创建新标签,并且我对旧对象 - >框架的引用保持在x = 0(即离开屏幕的位置)。也许是不知何故可以获得特定标签的坐标(基于与dateToAnnotate的比较)?-(void)axis:(CPTAxis *)axis labelWasSelected:(CPTAxisLabel *)label { NSLog(@"labelWasSelected: %@",label); _selectedLabel = label; CGRect graphLabelFrame = [self adjustAnnotationFrameForLabel:_selectedLabel]; [_delegate datesPlot:self didAnnotateFrame:graphLabelFrame animating:YES]; } -(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(nonnull CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate { CGRect graphLabelFrame = [self adjustAnnotationFrameForLabel:_selectedLabel]; [_delegate datesPlot:self didAnnotateFrame:graphLabelFrame animating:NO]; }
和在代表:
-(void)datesPlot:(datesPlot*) plot didAnnotateFrame:(CGRect)frame animating:(BOOL)animating{
if (animating) {
[UIView animateWithDuration:0.3 animations:^{
_annotationView.frame = [self.view convertRect:frame fromView:_datesHostingView];
}];
}
else {
_annotationView.frame = [self.view convertRect:frame fromView:_datesHostingView];
}
[_annotationView setNeedsLayout];
}
EDIT2: adjustAnnotation - 添加间隔和它的翻译成父视图的坐标之后转换帧图形坐标setNeedLayout
由委托调用。
-(CGRect)adjustAnnotationFrameForSelectedLabel {
NSLog(@"selected Label:\n %@", _selectedLabel);
float annotationMargin = 20;
CGRect labelFrame = _selectedLabel.contentLayer.frame;
NSLog(@"labelframe: x: %f, y: %f", labelFrame.origin.x, labelFrame.origin.y);
//add margin for annotation to label's frame
CGRect annotationFrame = CGRectMake(labelFrame.origin.x-annotationMargin/2, labelFrame.origin.y-annotationMargin/2,
labelFrame.size.width+annotationMargin, labelFrame.size.height+annotationMargin);
// convert from plot area coordinates to graph (and hosting view) coordinates
CGRect graphLabelFrame = [self.graph convertRect:annotationFrame fromLayer:self.graph.plotAreaFrame.plotArea];
NSLog(@"graphLabelFrame: x: %f, y: %f", graphLabelFrame.origin.x, graphLabelFrame.origin.y);
return graphLabelFrame;
}
检查链接http://stackoverflow.com/questions/19886928/willchangeplotrangeto-makes-no-difference-to-graphs-in-core-plot –