猎豆优选

LDTodayFlowLayout.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // LDTodayFlowLayout.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/13.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDTodayFlowLayout.h"
  9. #import "XHCollectionViewLayoutAttributes.h"
  10. #import "XHCollectionReusableView.h"
  11. @implementation LDTodayFlowLayout
  12. NSString *const TodayCollectionViewSectionBackground = @"TodayCollectionViewSectionBackground";
  13. #pragma mark ===============
  14. -(instancetype)init
  15. {
  16. self = [super init];
  17. if (self)
  18. {
  19. self.decorationViewAttrs = [NSMutableArray array];
  20. [self setup];
  21. }
  22. return self;
  23. }
  24. - (void)setup
  25. {
  26. [self registerClass:[XHCollectionReusableView class] forDecorationViewOfKind:TodayCollectionViewSectionBackground];
  27. }
  28. - (void)prepareLayout
  29. {
  30. [super prepareLayout];
  31. [self.decorationViewAttrs removeAllObjects];
  32. NSInteger numberOfSections = [self.collectionView numberOfSections];
  33. id delegate = self.collectionView.delegate;
  34. if (!numberOfSections || ![delegate conformsToProtocol:@protocol(TodayCollectionViewDelegateFlowLayout)]) {
  35. return;
  36. }
  37. for (NSInteger section = 0; section < numberOfSections; section++) {
  38. NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section];
  39. if (numberOfItems <= 0) {
  40. continue;
  41. }
  42. UICollectionViewLayoutAttributes *firstItem = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
  43. UICollectionViewLayoutAttributes *lastItem = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:numberOfItems - 1 inSection:section]];
  44. if (!firstItem || !lastItem) {
  45. continue;
  46. }
  47. UIEdgeInsets sectionInset = [self sectionInset];
  48. if ([delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
  49. UIEdgeInsets inset = [delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
  50. sectionInset = inset;
  51. }
  52. CGRect sectionFrame = CGRectUnion(firstItem.frame, lastItem.frame);
  53. sectionFrame.origin.x -= sectionInset.left;
  54. sectionFrame.origin.y -= sectionInset.top;
  55. if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
  56. sectionFrame.size.width += sectionInset.left + sectionInset.right;
  57. sectionFrame.size.height = self.collectionView.frame.size.height;
  58. } else {
  59. sectionFrame.size.width = self.collectionView.frame.size.width;
  60. sectionFrame.size.height += sectionInset.top + sectionInset.bottom;
  61. }
  62. // 2、定义
  63. XHCollectionViewLayoutAttributes *attr = [XHCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:TodayCollectionViewSectionBackground withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
  64. attr.frame = sectionFrame;
  65. attr.zIndex = -1;
  66. attr.backgroundColor = [delegate todayCollectionView:self.collectionView layout:self backgroundColorForSection:section];
  67. [self.decorationViewAttrs addObject:attr];
  68. }
  69. }
  70. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
  71. {
  72. NSMutableArray *attrs = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
  73. for (UICollectionViewLayoutAttributes *attr in self.decorationViewAttrs) {
  74. if (CGRectIntersectsRect(rect, attr.frame)) {
  75. [attrs addObject:attr];
  76. }
  77. }
  78. return attrs;
  79. }
  80. - (nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)elementKind atIndexPath:(NSIndexPath *)indexPath
  81. {
  82. if ([elementKind isEqualToString:TodayCollectionViewSectionBackground]) {
  83. return [self.decorationViewAttrs objectAtIndex:indexPath.section];
  84. }
  85. return [super layoutAttributesForDecorationViewOfKind:elementKind atIndexPath:indexPath];
  86. }
  87. @end