Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create UIButtons on CircleImage

I need to create Arch shaped UIButtons, on a circle image.(the O/p need to look like Concentric Circles),

At present I am using 5 images, but in future I may add some more Images, Dynamically I need to fil the circle Image with the added images.

I had a sample Piece of code and O/P is the below image

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

Image for Above Code

I have tried with this piece of code and the O/P is the below image

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

2nd Image

I need my output as the below image 3rd Image

How can i Achieve that

O/P after I Tried with Sarah Zuo's code

 Sarah Zuo Code

same width and height buttons Sarah Zuo code 2nd answer
AnyKind of Help is more Appreciated

like image 733
Srinivas Avatar asked Sep 25 '12 09:09

Srinivas


3 Answers

I can answer only last part,

If you are able to get images for buttons, then you can refer this tutorial. This might help you. Provided, your image formed is having transparent background.

like image 164
DivineDesert Avatar answered Nov 11 '22 12:11

DivineDesert


If all buttons' image look like the one (same direction), the transform value may be work.

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

button slice

like image 26
Sarah Zuo Avatar answered Nov 11 '22 14:11

Sarah Zuo


You can use your UIImageView instead. You can give different tag to image view and add Gesture Recognizer. Now you can get touch events like button click event on image views.

like image 21
Maulik Avatar answered Nov 11 '22 14:11

Maulik