1
我在我的第一个iPad项目中使用了Core Plot 0.9。我需要创建一个水平条形图,并在其左侧创建与每个条形图的中心对齐的UIButtons,每个条形图一个按钮,请参阅附件screenshot。将UIButtons对齐到水平核心图的条形图CPTBarPlot
我的设置是相当简单:
我有一个自定义UIView子类是作为一个容器。我有2个子视图:一个CPTGraphHostingView和一个UIView来包含我的UIButtons。 我在我的CPTGraphHostingView中托管一个CPTXYGraph,并分配一些填充值。
在我的自定义类,我有一次在数据源已定,将创建按钮的方法:
for (int i = 0; i < nbRecords; i++) {
// grab the bar location and create a plot point for it
NSNumber* loc = [dataSource numberForPlot:plot field:CPTBarPlotFieldBarLocation recordIndex:i];
double plotPoint[2] = {0, [loc doubleValue]};
// Convert the plot point to plot area space coordinates (I guess ;))
CGPoint viewPoint = [graph.defaultPlotSpace plotAreaViewPointForDoublePrecisionPlotPoint:plotPoint];
// Convert the view point to the button container layer coordinate system
CGPoint p = [graph convertPoint:viewPoint fromLayer:graph.plotAreaFrame.plotArea];
// Create the button
CGRect rect = CGRectMake(0, p.y - 11, 100, 22);
UIButton *btn = [[UIButton alloc] initWithFrame:rect];
[btn setTitle:@"test" forState:UIControlStateNormal];
[buttonView addSubview:btn];
[self addSubview:btn];
[array addObject:btn];
[btn release];
}
按钮被创建,但定位并不好......我发现,如果我设置所有图形填充为0,然后按钮对齐(但当然我需要填充)。
我错过了某些东西或点的转换步骤?我有点困惑如何将plotAreaPoint坐标转换为任何其他UIView ...!