UICollectionView 之 环形布局

环形布局

CollectionView.m------------------------

staticNSString*constreuseIdentifier =@"Cell";

NSIntegercellCount;

NSIntegerimageCount;

- (void)viewDidLoad {

[superviewDidLoad];

//设置开始的时候包含16个单元格

cellCount=16;

imageCount=1;

//创建自定义的布局对象

circleCollectionViewLayout* circleLayout=[[circleCollectionViewLayoutalloc]init];

//设置使用自定义的布局对象

self.collectionView.collectionViewLayout=circleLayout;

//设置背景色

self.collectionView.backgroundColor=[UIColorgrayColor];

//创建一个处理点击的手势处理器

UITapGestureRecognizer* tapRecognizer=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleTap:)];

// Uncomment the following line to preserve selection between presentations

// self.clearsSelectionOnViewWillAppear = NO;

self.collectionView.backgroundColor=[UIColorwhiteColor];

[self.collectionViewaddGestureRecognizer:tapRecognizer];

// Register cell classes

[self.collectionViewregisterClass:[myCollectionViewCellclass]forCellWithReuseIdentifier:reuseIdentifier];

// Do any additional setup after loading the view.

}

-(void)handleTap:(UITapGestureRecognizer*)sender

{

if(sender.state==UIGestureRecognizerStateEnded) {

//获取点击点的位置

CGPointinitialPinchPoint=[senderlocationInView:self.collectionView];

//获取点击点所在的NSIndexPath(可用于获取被点击的单元格)

NSIndexPath* tappedCellPath=[self.collectionViewindexPathForItemAtPoint:initialPinchPoint];

//如果被点击的单元格存在

if(tappedCellPath) {

cellCount--;//减少一个单元格

//删除被点击的单元格

[self.collectionViewdeleteItemsAtIndexPaths:[NSArrayarrayWithObject:tappedCellPath]];

}else{

cellCount++;

//在UICollectionView的开始处添加一个单元格

[self.collectionViewinsertItemsAtIndexPaths:@[[NSIndexPathindexPathForItem:0inSection:0]]];

}

}

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

#pragma mark

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView {

#warning Incomplete implementation, return the number of sections

return1;

}

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section {

#warning Incomplete implementation, return the number of items

returncellCount;

}

- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath {

myCollectionViewCell*cell = [collectionViewdequeueReusableCellWithReuseIdentifier:reuseIdentifierforIndexPath:indexPath];

cell.imageView.image=[UIImageimageNamed:[NSStringstringWithFormat:@"%d",imageCount%16]];

imageCount++;

// Configure the cell

returncell;

}

circleCollectionViewLayout.m---------------------------------

#define ITEM_SIZE72

@implementationcircleCollectionViewLayout

//开始执行的方法

-(void)prepareLayout{

[superprepareLayout];

CGSizesize=self.collectionView.frame.size;

//计算需要包含多少个单元格

_cellCount=[[selfcollectionView]numberOfItemsInSection:0];

//计算环的圆心

_center=CGPointMake(size.width/2.0, size.height/2.);

//计算环的半径

_radius=MIN(size.width, size.height)/2.5;

}

//该方法的返回值决定UICollectionView所包含控件的大小

-(CGSize)collectionViewContentSize{

return[selfcollectionView].frame.size;

}

//该方法返回的UICollectionViewLayoutAttributes控制制定单元格的大小和位置

-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath*)indexPath{

//创建一个UICollectionViewLayoutAttributes对象

UICollectionViewLayoutAttributes* attributes=[UICollectionViewLayoutAttributeslayoutAttributesForCellWithIndexPath:indexPath];

//设置个单元格的大小

attributes.size=CGSizeMake(ITEM_SIZE,ITEM_SIZE);

//设置改单元格的中心点坐标

//由于程序需要控制个单元格绕成一个圆圈,因此此处使用了三角函数进行计算

attributes.center=CGPointMake(_center.x+_radius*cosf(2*M_PI*indexPath.item/_cellCount),_center.y+_radius*sinf(2*M_PI*indexPath.item/_cellCount));

returnattributes;

}

//每当单元格动态显示时自动调用该方法

-(UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath*)itemIndexPath{

UICollectionViewLayoutAttributes* atributes=[selflayoutAttributesForItemAtIndexPath:itemIndexPath];

atributes.alpha=0.0;

atributes.center=CGPointMake(_center.x,_center.y);

returnatributes;

}

//每当单元格动态消失时自动调用该方法

-(UICollectionViewLayoutAttributes*)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath*)itemIndexPath{

UICollectionViewLayoutAttributes* attributes=[selflayoutAttributesForItemAtIndexPath:itemIndexPath];

attributes.alpha=0.0;

attributes.center=CGPointMake(_center.x,_center.y);

attributes.transform3D=CATransform3DMakeScale(0.1,0.1,1.0);

returnattributes;

}

@end

你可能感兴趣的:(UICollectionView 之 环形布局)