collecttionView实现头部视图悬停
控制器是UIViewController或者UICollectionViewController都可以使用,已经封装好了,直接可是在工程中使用。
1.重写UICollectionViewFlowLayout类。
新建类的.h文件
#import
@interface CatFlowLayout : UICollectionViewFlowLayout
@property (nonatomic , assign) CGFloat naviHeight;//默认为64.0,
@property (nonatomic , assign) int itemNum;//第几个item的头部悬浮
@property (nonatomic , assign) BOOL allItems;//是否全部悬浮,默认全部悬浮
@end
.m文件:
-(id)init
{
self = [super init];
if (self){
_naviHeight = 64.0;
_itemNum = 0;
_allItems = YES;
}
return self;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *superArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
NSMutableIndexSet *noneHeaderSections = [NSMutableIndexSet indexSet];
for (UICollectionViewLayoutAttributes *attributes in superArray)
{
if (attributes.representedElementCategory == UICollectionElementCategoryCell)
{
[noneHeaderSections addIndex:attributes.indexPath.section];
}
}
for (UICollectionViewLayoutAttributes *attributes in superArray)
{
if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader])
{
[noneHeaderSections removeIndex:attributes.indexPath.section];
}
}
[noneHeaderSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop){
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
if (attributes)
{
[superArray addObject:attributes];
}
}];
for (UICollectionViewLayoutAttributes *attributes in superArray) {
if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader])
{
NSInteger numberOfItemsInSection = [self.collectionView numberOfItemsInSection:attributes.indexPath.section];
NSIndexPath *firstItemIndexPath = [NSIndexPath indexPathForItem:0 inSection:attributes.indexPath.section];
NSIndexPath *lastItemIndexPath = [NSIndexPath indexPathForItem:MAX(0, numberOfItemsInSection-1) inSection:attributes.indexPath.section];
UICollectionViewLayoutAttributes *firstItemAttributes, *lastItemAttributes;
if (numberOfItemsInSection>0)
{
firstItemAttributes = [self layoutAttributesForItemAtIndexPath:firstItemIndexPath];
lastItemAttributes = [self layoutAttributesForItemAtIndexPath:lastItemIndexPath];
}else
{
firstItemAttributes = [UICollectionViewLayoutAttributes new];
CGFloat y = CGRectGetMaxY(attributes.frame)+self.sectionInset.top;
firstItemAttributes.frame = CGRectMake(0, y, 0, 0);
lastItemAttributes = firstItemAttributes;
}
CGRect rect = attributes.frame;
CGFloat offset = self.collectionView.contentOffset.y + _naviHeight;
CGFloat headerY = firstItemAttributes.frame.origin.y - rect.size.height - self.sectionInset.top;
CGFloat maxY = MAX(offset,headerY);
CGFloat headerMissingY = CGRectGetMaxY(lastItemAttributes.frame) + self.sectionInset.bottom - rect.size.height;
rect.origin.y = MIN(maxY,headerMissingY);
attributes.frame = rect;
attributes.zIndex = 7;
}
}
return [superArray copy];
}
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBound
{
return YES;
}
Controllder
2.在控制器中需要处理:
CatFlowLayout *flowLayout = [[CatFlowLayout alloc]init];
//设置头部视图的高度
flowLayout.naviHeight = 0;
//设置是否全部悬浮
flowLayout.allItems = YES;
//设置列间距
flowLayout.minimumLineSpacing = 1;
//设置行间距
flowLayout.minimumInteritemSpacing = 1;