LJCollectionViewFlowLayout.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // LJCollectionViewFlowLayout.m
  3. // Linkage
  4. //
  5. // Created by LeeJay on 16/8/22.
  6. // Copyright © 2016年 LeeJay. All rights reserved.
  7. // 代码下载地址https://github.com/leejayID/Linkage
  8. #import "LJCollectionViewFlowLayout.h"
  9. @implementation LJCollectionViewFlowLayout
  10. - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
  11. {
  12. // UICollectionViewLayoutAttributes:我称它为collectionView中的item(包括cell和header、footer这些)的《结构信息》
  13. // 截取到父类所返回的数组(里面放的是当前屏幕所能展示的item的结构信息),并转化成不可变数组
  14. NSMutableArray *superArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
  15. // 创建存索引的数组,无符号(正整数),无序(不能通过下标取值),不可重复(重复的话会自动过滤)
  16. NSMutableIndexSet *noneHeaderSections = [NSMutableIndexSet indexSet];
  17. // 遍历superArray,得到一个当前屏幕中所有的section数组
  18. for (UICollectionViewLayoutAttributes *attributes in superArray)
  19. {
  20. //如果当前的元素分类是一个cell,将cell所在的分区section加入数组,重复的话会自动过滤
  21. if (attributes.representedElementCategory == UICollectionElementCategoryCell)
  22. {
  23. [noneHeaderSections addIndex:attributes.indexPath.section];
  24. }
  25. }
  26. // 遍历superArray,将当前屏幕中拥有的header的section从数组中移除,得到一个当前屏幕中没有header的section数组
  27. // 正常情况下,随着手指往上移,header脱离屏幕会被系统回收而cell尚在,也会触发该方法
  28. for (UICollectionViewLayoutAttributes *attributes in superArray)
  29. {
  30. // 如果当前的元素是一个header,将header所在的section从数组中移除
  31. if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader])
  32. {
  33. [noneHeaderSections removeIndex:attributes.indexPath.section];
  34. }
  35. }
  36. // 遍历当前屏幕中没有header的section数组
  37. [noneHeaderSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *_Nonnull stop) {
  38. // 取到当前section中第一个item的indexPath
  39. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];
  40. // 获取当前section在正常情况下已经离开屏幕的header结构信息
  41. UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
  42. // 如果当前分区确实有因为离开屏幕而被系统回收的header
  43. if (attributes)
  44. {
  45. // 将该header结构信息重新加入到superArray中去
  46. [superArray addObject:attributes];
  47. }
  48. }];
  49. // 遍历superArray,改变header结构信息中的参数,使它可以在当前section还没完全离开屏幕的时候一直显示
  50. for (UICollectionViewLayoutAttributes *attributes in superArray)
  51. {
  52. // 如果当前item是header
  53. if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader])
  54. {
  55. // 得到当前header所在分区的cell的数量
  56. NSInteger numberOfItemsInSection = [self.collectionView numberOfItemsInSection:attributes.indexPath.section];
  57. // 得到第一个item的indexPath
  58. NSIndexPath *firstItemIndexPath = [NSIndexPath indexPathForItem:0 inSection:attributes.indexPath.section];
  59. // 得到最后一个item的indexPath
  60. NSIndexPath *lastItemIndexPath = [NSIndexPath indexPathForItem:MAX(0, numberOfItemsInSection - 1) inSection:attributes.indexPath.section];
  61. // 得到第一个item和最后一个item的结构信息
  62. UICollectionViewLayoutAttributes *firstItemAttributes, *lastItemAttributes;
  63. if (numberOfItemsInSection > 0)
  64. {
  65. // cell有值,则获取第一个cell和最后一个cell的结构信息
  66. firstItemAttributes = [self layoutAttributesForItemAtIndexPath:firstItemIndexPath];
  67. lastItemAttributes = [self layoutAttributesForItemAtIndexPath:lastItemIndexPath];
  68. }
  69. else
  70. {
  71. // cell没值,就新建一个UICollectionViewLayoutAttributes
  72. firstItemAttributes = [UICollectionViewLayoutAttributes new];
  73. // 然后模拟出在当前分区中的唯一一个cell,cell在header的下面,高度为0,还与header隔着可能存在的sectionInset的top
  74. CGFloat y = CGRectGetMaxY(attributes.frame) + self.sectionInset.top;
  75. firstItemAttributes.frame = CGRectMake(0, y, 0, 0);
  76. // 因为只有一个cell,所以最后一个cell等于第一个cell
  77. lastItemAttributes = firstItemAttributes;
  78. }
  79. // 获取当前header的frame
  80. CGRect rect = attributes.frame;
  81. // 当前的滑动距离 + 因为导航栏产生的偏移量,默认为64(如果app需求不同,需自己设置)
  82. CGFloat offset = self.collectionView.contentOffset.y;
  83. // 第一个cell的y值 - 当前header的高度 - 可能存在的sectionInset的top
  84. CGFloat headerY = firstItemAttributes.frame.origin.y - rect.size.height - self.sectionInset.top;
  85. // 哪个大取哪个,保证header悬停
  86. // 针对当前header基本上都是offset更加大,针对下一个header则会是headerY大,各自处理
  87. CGFloat maxY = MAX(offset, headerY);
  88. // 最后一个cell的y值 + 最后一个cell的高度 + 可能存在的sectionInset的bottom - 当前header的高度
  89. // 当当前section的footer或者下一个section的header接触到当前header的底部,计算出的headerMissingY即为有效值
  90. CGFloat headerMissingY = CGRectGetMaxY(lastItemAttributes.frame) + self.sectionInset.bottom - rect.size.height;
  91. // 给rect的y赋新值,因为在最后消失的临界点要跟谁消失,所以取小
  92. rect.origin.y = MIN(maxY, headerMissingY);
  93. // 给header的结构信息的frame重新赋值
  94. attributes.frame = rect;
  95. // 如果按照正常情况下,header离开屏幕被系统回收,而header的层次关系又与cell相等,如果不去理会,会出现cell在header上面的情况
  96. // 通过打印可以知道cell的层次关系zIndex数值为0,我们可以将header的zIndex设置成1,如果不放心,也可以将它设置成非常大,这里随便填了个7
  97. attributes.zIndex = 7;
  98. }
  99. }
  100. // 转换回不可变数组,并返回
  101. return [superArray copy];
  102. }
  103. // return YES:表示一旦滑动就实时调用上面这个layoutAttributesForElementsInRect:方法
  104. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBound
  105. {
  106. return YES;
  107. }
  108. @end