【问题标题】:How to align UIButtons within a circle?如何在一个圆圈内对齐 UIButtons?
【发布时间】:2011-10-14 08:24:34
【问题描述】:

例如,我有 20 个按钮。如何以循环格式添加此按钮的 UIView 的子视图?

【问题讨论】:

  • 参见this answer 了解使用 Swift 和 Auto Layout 约束的现代实现。

标签: iphone objective-c ios uibutton


【解决方案1】:

这会将按钮放置在给定中心的给定半径的圆周围。 它还将使用 UIView 的 transform 属性旋转每个按钮。

希望对您有所帮助。 :)

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

结果:

【讨论】:

  • @nacho4d 做反向操作?即恢复到 UIButton 的原始状态
  • 原始状态?我没有显示任何原始状态。你能说得更具体点吗?
  • 您将使用什么 curAngle 将数组的第一个按钮放在顶部视图的中间?
猜你喜欢
  • 2012-11-12
  • 2020-09-12
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
相关资源
最近更新 更多