【问题标题】:Programmatically create UIButtons on CircleImage以编程方式在 CircleImage 上创建 UIButton
【发布时间】:2012-09-25 09:12:19
【问题描述】:

我需要在圆形图像上创建拱形UIButtons。(O/p 需要看起来像同心圆),

目前我使用 5 张图片,但将来我可能会添加更多图片,动态我需要用添加的图片填充圆形图片。

我有一段示例代码,O/P 是下图

int numberOfSections = 6;
    CGFloat angleSize = 2*M_PI/numberOfSections;
 for (int j = 0; j < numberOfSections; ++j) {
        UIButton *sectionLabel = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 2.0f)];
sectionLabel.backgroundColor = [UIColor redColor];
 sectionLabel.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
        sectionLabel.layer.position = CGPointMake(container.bounds.size.width/2.0, container.bounds.size.height/2.0); // places anchorPoint of each label directly in the center of the circle.
        sectionLabel.transform = CGAffineTransformMakeRotation(angleSize*j);
        [container addSubview:sectionLabel];
    }

我试过这段代码,O/P 是下图

 int numberOfSections = 5;
    CGFloat angleSize = 2*M_PI/numberOfSections;
    for (int j = 0; j<numberOfSections; ++j) {
        UIImage *imageframe = [imagesArray objectAtIndex:j];
OBShapedButton *button = [[OBShapedButton alloc] initWithFrame:CGRectMake(0.0f, 150.0f, 150.0f, 128.0f)];
button.transform = CGAffineTransformMakeRotation(angleSize*j);
[container addSubview:button];
}

我需要如下图所示的输出

我怎样才能做到这一点

我尝试了 Sarah Zuo 的代码后的 O/P

相同的宽度和高度按钮
AnyKind 的帮助更受赞赏

【问题讨论】:

  • 新图像是否会被添加到同一个圆圈中,从而减少现有图像视图的当前弧度??
  • @AppleDelegate :是的,我们将计算图像数量以便进行划分

标签: iphone ios xcode geometry obshapedbutton


【解决方案1】:

我只能回答最后一部分,

如果您能够获取按钮的图像,那么您可以参考this tutorial。这可能会帮助你。前提是您形成的图像具有透明背景。

【讨论】:

  • @CrazyCreator : 我只是在放置按钮背景的图像,但是我怎样才能获得圆形类型的按钮。
  • 你永远无法创建圆形按钮,这就是你需要的技巧。保持其他部分透明,你的按钮看起来像圆形。
【解决方案2】:
float radius = container.bounds.size.width / 2;
int numberOfSections = 5;
CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j<numberOfSections; ++j) {
    UIImage *imageframe = [imagesArray objectAtIndex:j];
    OBShapedButton *button = [[OBShapedButton alloc] initWithFrame:CGRectMake(0.0f, 150.0f, 150.0f, 128.0f)];

    button.transform = CGAffineTransformMakeRotation(2*M_PI-angleSize*j);
    button.center = CGPointMake(radius + radius * sin(angleSize * j) / 2, radius +  radius * cos(angleSize * j) / 2);
    [container addSubview:button];
}

试试这个代码。

【讨论】:

  • 现在当我尝试使用你的代码时,我得到了五边形的内部部分
  • 当我尝试使用这段代码时,我将内部部分设为五边形,其中按钮位于圆圈的外部部分
  • 我已经更新了我的答案,你可以查看问题中的最后一个屏幕截图
  • 看来我为 button.center 和 button.transform 使用了错误的值。我固定了中心值,但按钮切片很混乱。
  • 是的,我希望,你能检查一次,如果可能的话,我正在尝试不同的值,我没有得到正确的输出你能检查一下吗
【解决方案3】:

您可以改用您的UIImageView。您可以将不同的tag 分配给图像视图并添加Gesture Recognizer。现在您可以在图像视图上获得触摸事件,例如按钮单击事件。

【讨论】:

  • 圆形图片是一个UIImageView,
【解决方案4】:

如果所有按钮的图像看起来都像一个(相同方向),则变换值可能有效。

button.transform = CGAffineTransformMakeRotation(2*M_PI-angleSize*j);
button.center = CGPointMake(radius + radius * sin(angleSize * j) / 2, radius +  radius * cos(angleSize * j) / 2);

【讨论】:

  • 虽然按钮有相同的宽度和高度我得到相同的类型,我也应该添加那个图像,请检查最后在 2 分钟内添加的图像
  • @Srinivas ,还有什么更好的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多