collectionView顶部header悬停效果

需要在商品列表顶部添加一个banner,并且添加分类标识按钮,要求滑动UICollectionView的时候banner滑动,而分类标识按钮悬停(最后有图)

方法步骤:

1.先创建UICollectionView

    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];

    layout.sectionHeadersPinToVisibleBounds = YES;//头视图悬浮

 metal_collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, SCREEN_WDITH, 0)       collectionViewLayout:layout];

 metal_collection.backgroundColor = [UIColor clearColor];

 metal_collection.delegate = self;

 metal_collection.dataSource = self;

 metal_collection.bounces = YES;

 metal_collection.alwaysBounceVertical = YES;//数据不够也可以垂直滑动

 metal_collection.showsVerticalScrollIndicator = YES;

    [self.view addSubview:metal_collection];

    [metal_collection registerClass:[TMetalProductCell class] forCellWithReuseIdentifier:@"MetalCollectionCell"];

    [metal_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MetalCollectionHead"];

    [metal_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MetalCollectionFooter"];

2.实现代理方法

#pragma -------------UICollectionDataSource-------------

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

 return 2;//这里很关键,分两组,把banner放在第一组的footer,把分类按钮放在第二组的header

}

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

{

 if (section == 0) {

 return 0;

    }

 return metal_Muarry.count;

}

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

{

 TMetalProductCell * cell = [metal_collection dequeueReusableCellWithReuseIdentifier:@"MetalCollectionCell" forIndexPath:indexPath];

    cell.metalModel = metal_Muarry[indexPath.item];

 return cell;

}

#pragma -------------UICollectionDelegateFlowLayout-------------

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

 if (section == 1) {

 return CGSizeMake(SCREEN_WDITH, 45);

    }

 return CGSizeZero;

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section

{

 if (section == 0) {

 return CGSizeMake(SCREEN_WDITH, 150);

    }

 return CGSizeZero;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

 if (indexPath.section == 1) {

 return CGSizeMake((SCREEN_WDITH - 5)/2, (SCREEN_WDITH - 5)/2 + 83);

    }

 return CGSizeZero;

}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{

 return 5;

}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

{

 return 5;

}

#pragma -------------UICollectionDelegate-------------

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

 TMetalProductModel * model = metal_Muarry[indexPath.item];

 TMetalProductDetailVC * transForVC =  [[TMetalProductDetailVC alloc]init];

    transForVC.metal_id = model.merchId;

    [self.navigationController pushViewController:transForVC animated:YES];

}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath*)indexPath

{

 if ([kind isEqualToString:UICollectionElementKindSectionFooter] && indexPath.section == 0) {

 UICollectionReusableView * footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MetalCollectionFooter" forIndexPath:indexPath];

 if (footerView.subviews.count == 0) {//加一个限制,避免无限创建新的 

 banner_Sc = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WDITH, 150)];

 banner_Sc.backgroundColor = [UIColor clearColor];

 banner_Sc.showsVerticalScrollIndicator = NO;

 banner_Sc.showsHorizontalScrollIndicator = NO;

 banner_Sc.pagingEnabled = YES;

 banner_Sc.delegate = self;

[footerView addSubview:banner_Sc];

        }

 return footerView;

    }else if ([kind isEqualToString:UICollectionElementKindSectionHeader] && indexPath.section == 1){

 UICollectionReusableView * headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MetalCollectionHead" forIndexPath:indexPath];

 if (headView.subviews.count == 0) {//加一个限制,避免无限创建新的 

 HTScrollMenuView * menuView = [[HTScrollMenuView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WDITH, 40) withItem:@[@"工艺类",@"钱币类",@"首饰类",@"投资类"] withDelegate:self];

[headView addSubview:menuView];

        }

 return headView;

    }

 return nil;

}

转载于:https://www.cnblogs.com/chenweb/p/10802944.html

iOS的CollectionView是一种强大的界面展示控件,它允许我们展示多个横向或纵向滚动的项。而Section HeaderCollectionView中的一个重要组成部分,它可以用来展示每个分区的标题或其他相关信息。 在iOS中使用CollectionView的时候,我们可以通过实现UICollectionViewDelegateFlowLayout协议来设置每个分区的大小、间距等布局属性。而Section Header则需要通过实现UICollectionViewDelegate的collectionView:viewForSupplementaryElementOfKind:atIndexPath:方法来自定义。 首先,我们需要创建一个UICollectionReusableView类的子类作为Section Header的视图。然后,在collectionView:viewForSupplementaryElementOfKind:atIndexPath:方法中,我们需要判断elementKind是否为UICollectionElementKindSectionHeader,并根据需要从重用队列中获取Section Header的视图对象。 接下来,我们可以设置Section Header的标题、背景色、字体颜色等视觉效果。例如,我们可以使用UILabel来展示标题,并通过设置UILabel的text属性来显示每个分区的标题。如果需要更加丰富的自定义效果,我们还可以使用自定义的视图来展示Section Header。 最后,我们需要在UICollectionViewFlowLayout中设置sectionHeadersPinToVisibleBounds属性为true,以便在滚动时固定Section Header的位置。这样,当用户滚动CollectionView时,Section Header会始终显示在顶部,增加了用户的使用体验。 综上所述,iOS的CollectionView提供了强大的支持来展示多个滚动项,而Section Header则可以用来展示每个分区的相关信息。通过实现UICollectionViewDelegate的collectionView:viewForSupplementaryElementOfKind:atIndexPath:方法,我们可以自定义Section Header的视图,并通过UICollectionViewFlowLayout来设置其布局等属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值