Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Say for example I have 20 buttons. How can I add a subView of the UIView of this button in a circular format?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
551 views
Welcome To Ask or Share your Answers For Others

1 Answer

This will place the buttons around a circle of a given radius at a given center. It will also rotate each button using the transform property of UIView.

Hope it helps. :)

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *buttons = /* you buttons here */
    float curAngle = 0;
    float incAngle = ( 360.0/(buttons.count) )*PI/180.0;
    CGPoint circleCenter = CGPointMake(160, 200); /* given center */
    float circleRadius = 100; /* given radius */
    for (UIButton *button in buttons)
    {
        CGPoint buttonCenter;
        buttonCenter.x = circleCenter.x + cos(curAngle)*circleRadius;
        buttonCenter.y = circleCenter.y + sin(curAngle)*circleRadius;
        button.transform = CGAffineTransformRotate(button.transform, curAngle);
        button.center = buttonCenter;
        [self.view addSubview:button];

        curAngle += incAngle;
    }
}

Result:

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...