//1.使用UICollectionView做成上面图片的效果
- 1.首先创建一个类 继承自UICollectionViewLayout
@interface BOLayOut : UICollectionViewLayout
///存储有多少个item
@property(nonatomic,assign)int itemcount;
@end```
- 2 .m文件的实现
import "BOLayOut.h"
@implementation BOLayOut{
NSMutableArray * _nmArr;
}
-(void)prepareLayout{
[super prepareLayout];
//1.获取item的个数
self.itemcount = (int)[self.collectionView numberOfItemsInSection:0];
// NSLog(@"%d",self.itemcount);
_nmArr = [NSMutableArray array];
//2.取大圆的宽高最短的那个边
CGFloat radius = MIN(self.collectionView.frame.size.width, self.collectionView.frame.size.height)/2;
CGPoint center = CGPointMake(self.collectionView.frame.size.width/2, self.collectionView.frame.size.height/2);
NSLog(@"---%f,%f",center.x,center.y);
//3.设置每个item的大小,大小设定为50*50
for (int i = 0; i<self.itemcount; i++) {
UICollectionViewLayoutAttributes * attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
//设置item的大小
attributes.size = CGSizeMake(50, 50);
//计算每个item的圆心的位置
float x = center.x + cosf(2*M_PI /self.itemcount * i)*(radius - 25);
float y = center.y + sinf(2*M_PI /self.itemcount * i)*(radius - 25);
attributes.center = CGPointMake(x, y);
NSLog(@"%f,%f",attributes.center.x,attributes.center.y);
[_nmArr addObject:attributes];
}
}
//设置内容区域的大小
-(CGSize)collectionViewContentSize{
return self.collectionView.frame.size;
}
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
return _nmArr;
}```
- 3 viewController的实现
#import "ViewController.h"
#import "BOLayOut.h"
static NSString * reuseId = @"cell";
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
BOLayOut * layout = [[BOLayOut alloc]init];
UICollectionView * collect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height/2) collectionViewLayout:layout];
collect.delegate = self;
collect.dataSource = self;
[collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseId];
[self.view addSubview:collect];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 20;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseId forIndexPath:indexPath];
cell.layer.cornerRadius = 25;
cell.layer.masksToBounds = YES;
cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
return cell;
}```
以上是简单的实现,有不足请指出