比方说,我有20个按钮。如何以圆形格式添加此按钮的UIView的子视图?
答案 0 :(得分:14)
这会将按钮放在给定中心的给定半径的圆周围。
它还将使用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;
}
}
<强>结果:强>