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?
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++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With