環形布局
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