主要是在于对layer的理解。view的底层是layer。所以控制位置的其实是layer。用layer的anchorPoint可以做出一个绕中心轴旋转的效果。如下图:
5BDF1CC5-F9CF-4012-9659-7ECC5145B4E0.png
上代码
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger count = 24;
for (int i = 0; i < count; i++) {
UIView *greenview = [[UIView alloc]init];
greenview.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenview];
//利用anchorPoint来控制view的位置
greenview.bounds = CGRectMake(0, 0, 25, 40);
greenview.layer.anchorPoint = CGPointMake(0.5, 4.0);
greenview.center = self.view.center;
CGFloat radion = (360/count * i) / 180.0 * M_PI;
greenview.transform = CGAffineTransformMakeRotation(radion);
}
}