2016-06-27 29 views
0

我在x轴上使用日期标签,并希望使用自定义视图高亮显示特定日期。我打算用axis:labelWasSelected:,移动annotationFrame到标签的位置和滚动它时,轴范围变化(呈三角core plot Framework - How to set custom UIView in CPTPlotSpaceAnnotation)(移动在plotSpacewillChangePlotRangeToForCoordinate:注释)Coreplot - 轴标签的坐标和大小

我怎样才能获得标签的坐标从plotSpacewillChangePlotRangeToForCoordinate调用时?

enter image description here

编辑: 我已经成功实施移动标注由用户选择调用:我保存_selectedLabel和发送newFrame的时候的情节变化范围。在2个问题,这:变化范围在注释框架的

  1. 延迟 - 看的电影:https://www.youtube.com/watch?v=R66ew6GAiJU&feature=youtu.be

  2. 当注释标签离开屏幕时,_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; 

} 
+0

检查链接http://stackoverflow.com/questions/19886928/willchangeplotrangeto-makes-no-difference-to-graphs-in-core-plot –

回答

1

的第二个参数的方法-axis:labelWasSelected:是轴标签。标签的contentLayer是实际显示标签的CALayer子类。使用内置的坐标转换方法将contentLayerframe和/或bounds转换为注释的annotationHostLayer的坐标空间。

+0

您的解决方案工作,但我仍然有一些问题。请参阅编辑。 – izik461

+0

1.标签层上的'-adjustAnnotationFrameForLabel:'调用'-layoutIfNeeded'确保它的'frame'在读取之前是最新的? –

+0

2. Core Plot标签策略在标签不再可见时删除标签以节省内存。您可以使用“无”或“提供位置”政策来更好地控制标签的位置。否则,在“willChange”委托方法中查看新的绘图区域。如果注释位置超出该范围,请将其隐藏。如果它返回到绘图区域,请查看每个标签的'tickLocation'以查找与注释位置匹配的标签。 –