2011-10-20 43 views
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 ...!

回答

0

好的,我自己想出了這個問題。事實上,我在圖形有機會佈局之前添加了按鈕。在循環解決對齊問題之前調用圖的方法-layoutIfNeeded: