扎堆布局效果
//
// StackLayout.h
// 自定义UICellectionView布局
#import <UIKit/UIKit.h>
@interface StackLayout : UICollectionViewLayout
@end
//
// StackLayout.m
// 自定义UICellectionView布局
#import "StackLayout.h"
@implementation StackLayout
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *angles = @[@0, @(-0.2), @(-0.5), @(0.2), @(0.5)];
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attrs.size = CGSizeMake(100, 100);
attrs.center = CGPointMake(self.collectionView.frame.size.width * 0.5, self.collectionView.frame.size.height * 0.5);
if (indexPath.item >= 5) {
attrs.hidden = YES;
} else {
attrs.transform = CGAffineTransformMakeRotation([angles[indexPath.item] floatValue]);
// zIndex越大,就越在上面
attrs.zIndex = [self.collectionView numberOfItemsInSection:indexPath.section] - indexPath.item;
}
return attrs;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *array = [NSMutableArray array];
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (int i = 0; i<count; i++) {
UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
[array addObject:attrs];
}
return array;
}
@end