Coreplot - 轴标签的坐标和大小

时间:2016-06-27 11:51:25

标签: ios objective-c core-plot

我在x轴上使用日期标签,并希望使用自定义视图突出显示特定日期。我打算使用axis:labelWasSelected:,将annotationFrame移动到标签位置并在轴范围更改时滚动它(在plotSpacewillChangePlotRangeToForCoordinate:中移动注释)(simillar to core plot Framework - How to set custom UIView in CPTPlotSpaceAnnotation

从plotSpacewillChangePlotRangeToForCoordinate调用时,如何获取标签的坐标?

enter image description here

编辑: 我已经成功实现了用户选择调用的移动注释:我保存_selectedLabel并在绘图范围更改时发送newFrame。这有两个问题:

  1. 更改范围时延迟注释框架 - 请参阅电影:https://www.youtube.com/watch?v=R66ew6GAiJU&feature=youtu.be

  2. 当带注释的标签离开屏幕时,_selectedLabel被遗忘"。当回到屏幕时,可能会创建新标签,并且我对旧对象 - >框架的_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];
    }
    
  3. 并在代表中:

    -(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;
    
    }
    

1 个答案:

答案 0 :(得分:1)

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