首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CircleImage上以编程方式创建UIButtons

在CircleImage上以编程方式创建UIButtons
EN

Stack Overflow用户
提问于 2012-09-25 17:12:20
回答 4查看 823关注 0票数 5

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

目前我正在使用5个图像,但在未来我可能会添加更多的图像,动态我需要用添加的图像填充圆形图像。

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

代码语言:javascript
复制
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如下图所示

代码语言:javascript
复制
 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的代码后的操作

相同宽度和高度的按钮

AnyKind of Help更受欢迎

EN

回答 4

Stack Overflow用户

发布于 2012-09-25 17:47:22

我只能回答后半部分,

如果你能够获得按钮的图片,那么你可以参考this tutorial。这可能会对你有帮助。如果你的图像具有透明的背景,你的图像就会形成。

票数 4
EN

Stack Overflow用户

发布于 2012-09-25 17:57:58

代码语言:javascript
复制
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];
}

试试这段代码。

票数 2
EN

Stack Overflow用户

发布于 2012-09-25 17:18:30

您可以使用您的UIImageView。您可以为图片视图赋予不同的tag,并添加Gesture Recognizer。现在,您可以在图像视图上获得触摸事件,如按钮单击事件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12579733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档