2017-09-28
- (void)prepareLayout { [super prepareLayout]; self.lastPoint = CGPointZero; self.AttrList = [NSMutableArray array]; NSUInteger sectionCount = [self.collectionView numberOfSections]; for (NSInteger index = 0; index < sectionCount; index++) { NSInteger hotItemCount = [self.collectionView numberOfItemsInSection:index]; if (index != sectionCount-1) { //最后一个区不需header [self.AttrList addObject:[self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:index]]]; } for (int i =0; i <hotItemCount; i++) { UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:index]]; [self.AttrList addObject:attributes]; } UICollectionViewLayoutAttributes *hotLastAttributes = self.AttrList.lastObject; self.lastPoint = CGPointMake(0, hotLastAttributes.frame.size.height +hotLastAttributes.frame.origin.y +[self itemVerticalPadding]); } } - (CGSize)collectionViewContentSize { return CGSizeMake(SCREENWIDTH-20, self.lastPoint.y); } - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect { return [self.AttrList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) { return CGRectIntersectsRect(rect, [evaluatedObject frame]); }]]; } - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath { UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath]; attributes.frame = CGRectMake(0, self.lastPoint.y, SCREENWIDTH-20, _sectionHeaderHeight); self.lastPoint = CGPointMake(0, self.lastPoint.y +_sectionHeaderHeight); return attributes; } - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; CGFloat itemVerticalPadding = [self itemVerticalPadding]; CGFloat itemHorizontalPadding = [self itemHorizontalPadding]; CGSize itemTextSize = [self.delegate itemTextSizeIndexPath:indexPath]; CGFloat itemWidth = itemTextSize.width +staticItemHotInsets.left +staticItemHotInsets.right; CGFloat itemHeight = itemTextSize.height +staticItemHotInsets.top +staticItemHotInsets.bottom; if (self.lastPoint.x +itemHorizontalPadding +itemWidth >=SCREENWIDTH-20) { UICollectionViewLayoutAttributes *lastAttributes = self.AttrList.lastObject; self.lastPoint = CGPointMake(0, lastAttributes.frame.size.height +lastAttributes.frame.origin.y); } CGRect rect = CGRectMake(self.lastPoint.x +itemHorizontalPadding, self.lastPoint.y +itemVerticalPadding, itemWidth, itemHeight); attributes.frame = rect; self.lastPoint = CGPointMake(rect.origin.x +rect.size.width, self.lastPoint.y); return attributes; } - (CGFloat)itemHorizontalPadding { if (self.delegate && [self.delegate respondsToSelector:@selector(customItemHorizontalPadding)]) return [self.delegate customItemHorizontalPadding]; return 10; } - (CGFloat)itemVerticalPadding { if (self.delegate && [self.delegate respondsToSelector:@selector(customItemVerticalPadding)]) return [self.delegate customItemVerticalPadding]; return 10; } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return NO; }