123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- //
- // LDTodayFlowLayout.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/12/13.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "LDTodayFlowLayout.h"
- #import "XHCollectionViewLayoutAttributes.h"
- #import "XHCollectionReusableView.h"
- @implementation LDTodayFlowLayout
- NSString *const TodayCollectionViewSectionBackground = @"TodayCollectionViewSectionBackground";
- #pragma mark ===============
- -(instancetype)init
- {
- self = [super init];
- if (self)
- {
- self.decorationViewAttrs = [NSMutableArray array];
- [self setup];
- }
- return self;
- }
- - (void)setup
- {
- [self registerClass:[XHCollectionReusableView class] forDecorationViewOfKind:TodayCollectionViewSectionBackground];
- }
- - (void)prepareLayout
- {
- [super prepareLayout];
- [self.decorationViewAttrs removeAllObjects];
-
- NSInteger numberOfSections = [self.collectionView numberOfSections];
- id delegate = self.collectionView.delegate;
- if (!numberOfSections || ![delegate conformsToProtocol:@protocol(TodayCollectionViewDelegateFlowLayout)]) {
- return;
- }
-
- for (NSInteger section = 0; section < numberOfSections; section++) {
- NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section];
- if (numberOfItems <= 0) {
- continue;
- }
- UICollectionViewLayoutAttributes *firstItem = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
- UICollectionViewLayoutAttributes *lastItem = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:numberOfItems - 1 inSection:section]];
- if (!firstItem || !lastItem) {
- continue;
- }
-
- UIEdgeInsets sectionInset = [self sectionInset];
-
- if ([delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
- UIEdgeInsets inset = [delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:section];
- sectionInset = inset;
- }
-
-
- CGRect sectionFrame = CGRectUnion(firstItem.frame, lastItem.frame);
- sectionFrame.origin.x -= sectionInset.left;
- sectionFrame.origin.y -= sectionInset.top;
-
- if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
- sectionFrame.size.width += sectionInset.left + sectionInset.right;
- sectionFrame.size.height = self.collectionView.frame.size.height;
- } else {
- sectionFrame.size.width = self.collectionView.frame.size.width;
- sectionFrame.size.height += sectionInset.top + sectionInset.bottom;
- }
-
- // 2、定义
- XHCollectionViewLayoutAttributes *attr = [XHCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:TodayCollectionViewSectionBackground withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
- attr.frame = sectionFrame;
- attr.zIndex = -1;
-
- attr.backgroundColor = [delegate todayCollectionView:self.collectionView layout:self backgroundColorForSection:section];
- [self.decorationViewAttrs addObject:attr];
- }
-
- }
- - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
- {
- NSMutableArray *attrs = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
- for (UICollectionViewLayoutAttributes *attr in self.decorationViewAttrs) {
-
- if (CGRectIntersectsRect(rect, attr.frame)) {
- [attrs addObject:attr];
- }
- }
- return attrs;
- }
- - (nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)elementKind atIndexPath:(NSIndexPath *)indexPath
- {
- if ([elementKind isEqualToString:TodayCollectionViewSectionBackground]) {
- return [self.decorationViewAttrs objectAtIndex:indexPath.section];
- }
- return [super layoutAttributesForDecorationViewOfKind:elementKind atIndexPath:indexPath];
- }
- @end
|