简体   繁体   English

如何在圆圈内对齐 UIButton?

[英]How to align UIButtons within a circle?

Say for example I have 20 buttons.比如说我有 20 个按钮。 How can I add a subView of the UIView of this button in a circular format?如何以循环格式添加此按钮的 UIView 的子视图?

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.它还将使用 UIView 的transform属性旋转每个按钮。

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:结果:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM