我在我的第一个iPad项目中使用了Core plot0.9。我需要创建一个水平条形图,并在其左侧创建与每个条形的中心对齐的UIButtons,每个条形一个按钮,cf附加的screenshot。
我的设置非常简单:
我有一个自定义的UIView子类,它充当容器。在它内部,我有两个子视图:一个CPTGraphHostingView和一个包含我的UIButtons的UIView。我在CPTGraphHostingView中托管了一个CPTXYGraph,并分配了一些填充值。
在我的自定义类中,我有一个方法,一旦设置了dataSource,它就会创建按钮:
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有点困惑…!
发布于 2011-11-03 17:43:54
好吧,我自己想出了问题所在。事实上,我是在图表有机会自己布局之前添加按钮的。在循环之前调用图形的方法-layoutIfNeeded:解决了对齐问题
https://stackoverflow.com/questions/7832464
复制相似问题