**collecttionView实现头部视图悬停**

本文介绍如何在iOS应用中使用UICollectionView实现头部视图悬停功能。通过自定义UICollectionViewFlowLayout子类,设置头部视图的高度、是否全部悬浮以及间距等参数,可以轻松地在项目中实现这一效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值