Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Arrange subviews in circular shape

I have a bunch of views in my app. I would like to arrange them in a circular shape and change their center depending on the number of views present.

So, if there are 3 views they would look like a triangle, but would still form a circle. If there are 4 it would look like a square but still form a circle, and so on...

In short, the centers of all views would sit on a imaginary circle.

Any suggestions?

like image 251
peace4theapes Avatar asked Jul 17 '12 22:07

peace4theapes


1 Answers

This is the code I used in one of my projects, hope it helps.

// you must set both of these
CGPoint centerOfCircle;
float radius;

int count = 0;
float angleStep = 2.0f * M_PI / [arrayOfViews count];

for (UIView *view in arrayOfViews) {
    float xPos = cosf(angleStep * count) * radius;
    float yPos = sinf(angleStep * count) * radius;
    view.center = CGPointMake(centerOfCircle.x + xPos, centerOfCircle.y +yPos);
    count++;
}
like image 113
Craig Siemens Avatar answered Sep 24 '22 17:09

Craig Siemens