我在x轴上使用日期标签,并希望使用自定义视图突出显示特定日期。我打算使用axis:labelWasSelected:
,将annotationFrame移动到标签位置并在轴范围更改时滚动它(在plotSpacewillChangePlotRangeToForCoordinate:
中移动注释)(simillar to core plot Framework - How to set custom UIView in CPTPlotSpaceAnnotation)
从plotSpacewillChangePlotRangeToForCoordinate调用时,如何获取标签的坐标?
编辑:
我已经成功实现了用户选择调用的移动注释:我保存_selectedLabel
并在绘图范围更改时发送newFrame。这有两个问题:
更改范围时延迟注释框架 - 请参阅电影:https://www.youtube.com/watch?v=R66ew6GAiJU&feature=youtu.be
当带注释的标签离开屏幕时,_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];
}
并在代表中:
-(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 :(得分:1)
-axis:labelWasSelected:
方法的第二个参数是轴标签。标签的contentLayer
是实际显示标签的CALayer
子类。使用内置坐标转换方法将frame
的{{1}}和/或bounds
转换为注释contentLayer
的坐标空间。