2016-06-27 129 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'以查找與註釋位置匹配的標籤。 –