贡献做者 -【XJDomain】
博客XJ: https://my.oschina.net/shengbingli/blog
GitHub: https://github.com/lishengbing/XJQRCodeToolDemogit
1:tableview有自动头部和底部吸顶的功能,collectionView也是能够作到的
2:将collectionView的 flowLayout 换成个人自定义类的对象便可github
使用范例:swift
//固定头部layout SectionHeadercCollectionViewLayout *layout = [[SectionHeadercCollectionViewLayout alloc] init]; //不固定头部 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// // SectionHeadercCollectionViewLayout.h // XDSectionCollectionView // // Created by 李胜兵 on 15/9/9. // Copyright (c) 2015年 李胜兵. All rights reserved. // #import <UIKit/UIKit.h> @interface SectionHeadercCollectionViewLayout : UICollectionViewFlowLayout @end
// // SectionHeadercCollectionViewLayout.m // XDSectionCollectionView // // Created by 李胜兵 on 15/9/9. // Copyright (c) 2015年 李胜兵. All rights reserved. // #import "SectionHeadercCollectionViewLayout.h" /** * SectionHeader能够像tableView那样悬浮在顶部 * 继承于UICollectionViewFlowLayout,用法与collectionViewFlowLayout同样 */ @implementation SectionHeadercCollectionViewLayout - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect { //UICollectionViewLayoutAttributes:我称它为collectionView中的item(包括cell和header、footer这些)的《结构信息》 //截取到父类所返回的数组(里面放的是当前屏幕所能展现的item的结构信息),并转化成不可变数组 NSMutableArray *superArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy]; //建立存索引的数组,无符号(正整数),无序(不能经过下标取值),不可重复(重复的话会自动过滤) NSMutableIndexSet *noneHeaderSections = [NSMutableIndexSet indexSet]; //遍历superArray,获得一个当前屏幕中全部的section数组 for (UICollectionViewLayoutAttributes *attributes in superArray) { //若是当前的元素分类是一个cell,将cell所在的分区section加入数组,重复的话会自动过滤 if (attributes.representedElementCategory == UICollectionElementCategoryCell) { [noneHeaderSections addIndex:attributes.indexPath.section]; } } //遍历superArray,将当前屏幕中拥有的header的section从数组中移除,获得一个当前屏幕中没有header的section数组 //正常状况下,随着手指往上移,header脱离屏幕会被系统回收而cell尚在,也会触发该方法 for (UICollectionViewLayoutAttributes *attributes in superArray) { //若是当前的元素是一个header,将header所在的section从数组中移除 if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) { [noneHeaderSections removeIndex:attributes.indexPath.section]; } } //遍历当前屏幕中没有header的section数组 [noneHeaderSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop){ //取到当前section中第一个item的indexPath NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx]; //获取当前section在正常状况下已经离开屏幕的header结构信息 UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath]; //若是当前分区确实有由于离开屏幕而被系统回收的header if (attributes) { //将该header结构信息从新加入到superArray中去 [superArray addObject:attributes]; } }]; //遍历superArray,改变header结构信息中的参数,使它能够在当前section还没彻底离开屏幕的时候一直显示 for (UICollectionViewLayoutAttributes *attributes in superArray) { //若是当前item是header if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) { //获得当前header所在分区的cell的数量 NSInteger numberOfItemsInSection = [self.collectionView numberOfItemsInSection:attributes.indexPath.section]; //获得第一个item的indexPath NSIndexPath *firstItemIndexPath = [NSIndexPath indexPathForItem:0 inSection:attributes.indexPath.section]; //获得最后一个item的indexPath NSIndexPath *lastItemIndexPath = [NSIndexPath indexPathForItem:MAX(0, numberOfItemsInSection-1) inSection:attributes.indexPath.section]; //获得第一个item和最后一个item的结构信息 UICollectionViewLayoutAttributes *firstItemAttributes, *lastItemAttributes; if (numberOfItemsInSection>0) { //cell有值,则获取第一个cell和最后一个cell的结构信息 firstItemAttributes = [self layoutAttributesForItemAtIndexPath:firstItemIndexPath]; lastItemAttributes = [self layoutAttributesForItemAtIndexPath:lastItemIndexPath]; }else { //cell没值,就新建一个UICollectionViewLayoutAttributes firstItemAttributes = [UICollectionViewLayoutAttributes new]; //而后模拟出在当前分区中的惟一一个cell,cell在header的下面,高度为0,还与header隔着可能存在的sectionInset的top CGFloat y = CGRectGetMaxY(attributes.frame)+self.sectionInset.top; firstItemAttributes.frame = CGRectMake(0, y, 0, 0); //由于只有一个cell,因此最后一个cell等于第一个cell lastItemAttributes = firstItemAttributes; } //获取当前header的frame CGRect rect = attributes.frame; //当前的滑动距离 + 由于导航栏产生的偏移量,默认为64(若是app需求不一样,需本身设置) CGFloat offset = self.collectionView.contentOffset.y; //第一个cell的y值 - 当前header的高度 - 可能存在的sectionInset的top CGFloat headerY = firstItemAttributes.frame.origin.y - rect.size.height - self.sectionInset.top; //哪一个大取哪一个,保证header悬停 //针对当前header基本上都是offset更加大,针对下一个header则会是headerY大,各自处理 CGFloat maxY = MAX(offset,headerY); //最后一个cell的y值 + 最后一个cell的高度 + 可能存在的sectionInset的bottom - 当前header的高度 //当当前section的footer或者下一个section的header接触到当前header的底部,计算出的headerMissingY即为有效值 CGFloat headerMissingY = CGRectGetMaxY(lastItemAttributes.frame) + self.sectionInset.bottom - rect.size.height; //给rect的y赋新值,由于在最后消失的临界点要跟谁消失,因此取小 rect.origin.y = MIN(maxY,headerMissingY); //给header的结构信息的frame从新赋值 attributes.frame = rect; //若是按照正常状况下,header离开屏幕被系统回收,而header的层次关系又与cell相等,若是不去理会,会出现cell在header上面的状况 //经过打印能够知道cell的层次关系zIndex数值为0,咱们能够将header的zIndex设置成1,若是不放心,也能够将它设置成很是大,这里随便填了个7 attributes.zIndex = 7; } } //转换回不可变数组,并返回 return [superArray copy]; } //return YES;表示一旦滑动就实时调用上面这个layoutAttributesForElementsInRect:方法 - (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBound { return YES; } @end
若是你以为对你有用,请你star,谢谢
若是你以为之后对你仍是有点用,也请你star、谢谢数组
// 强大的二维码一键搞定
https://github.com/lishengbing/XJQRCodeToolDemoapp
// 强大的swift3.0版本侧滑全功能、第三方库都知足不了个人要求
https://github.com/lishengbing/swift3.0-XJDragerDemospa
// 强大的oc版本抽屉效果、第三方库都知足不了个人要求
https://github.com/lishengbing/XJDragViewController.net