将 UIButtons 与水平核心图 CPTBarPlot 的条形图对齐

发布于 2024-12-10 22:44:14 字数 1450 浏览 0 评论 0 原文

我在我的第一个 iPad 项目中使用 Core Plot 0.9。我需要创建一个水平条形图,并在其左侧创建与每个条形中心对齐的 UIButtons,每个条形一个按钮,参见附件 屏幕截图

我的设置非常简单:

我有一个充当容器的自定义 UIView 子类。 在它的内部,我有 2 个子视图:一个 CPTGraphHostingView 和一个包含我的 UIButtons 的 UIView。 我在 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有点困惑......!

I'm using Core Plot 0.9 in my first iPad project. I need to create an horizontal bar plot, and to create UIButtons on its left aligned with the center of each bar, one button per bar, cf the attached screenshot.

My setup is pretty simple:

I have a custom UIView subclass that acts as a container.
Inside of it, I have 2 subviews: a CPTGraphHostingView and a UIView to contain my UIButtons.
I host a CPTXYGraph in my CPTGraphHostingView and I assign some paddings values.

In my custom class, I have a method that will create the buttons once the dataSource has been set:

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];
}

The buttons are created but the alignment is not good... I noticed that if I set all the graph paddings to 0, then the buttons are aligned (but of course I need the padding).

Am I missing something or a step in the conversion of the points ? I am a bit confused on how to convert a plotAreaPoint coordinates to any other UIView ...!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2024-12-17 22:44:14

好吧,我自己发现了问题。事实上,我是在图表有机会自行布局之前添加按钮的。在循环之前调用图形的方法 -layoutIfNeeded: 解决了对齐问题

Ok I figured the problem myself. In fact, I was adding the buttons before the graph had a chance to layout itself. Calling the graph's method -layoutIfNeeded: just before the loop solved the alignment problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文