多个view360度围绕中心点旋转
2017-05-17 本文已影响115人
charlotte2018
主要是在于对layer的理解。view的底层是layer。所以控制位置的其实是layer。用layer的anchorPoint可以做出一个绕中心轴旋转的效果。如下图:

上代码
- (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);
}
}