我在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 –