From 11ad8f68ab71fdf3abae8a8d61be305defb1dc64 Mon Sep 17 00:00:00 2001 From: tangyumeng Date: Fri, 18 Jan 2019 18:52:24 +0800 Subject: [PATCH] [feature] enable sticky header --- CHTCollectionViewWaterfallLayout.h | 10 ++++++ CHTCollectionViewWaterfallLayout.m | 34 ++++++++++++++++--- Demo/Objective-C/Demo/ViewController.m | 2 +- .../en.lproj/MainStoryboard_iPhone.storyboard | 23 ++++++------- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/CHTCollectionViewWaterfallLayout.h b/CHTCollectionViewWaterfallLayout.h index 4788aed..f9e3e6a 100644 --- a/CHTCollectionViewWaterfallLayout.h +++ b/CHTCollectionViewWaterfallLayout.h @@ -297,6 +297,16 @@ extern NSString *const CHTCollectionElementKindSectionFooter; */ @property (nonatomic, assign) UIEdgeInsets sectionInset; + +/** + * @brief control when user scroll the collecitonView , sectionHeader stay at top the screen + * + * + */ +@property (nonatomic, assign) BOOL enableStickyHeader; + + + /** * @brief The direction in which items will be rendered in subsequent rows. * @discussion diff --git a/CHTCollectionViewWaterfallLayout.m b/CHTCollectionViewWaterfallLayout.m index 1a5bd12..0a24a4b 100644 --- a/CHTCollectionViewWaterfallLayout.m +++ b/CHTCollectionViewWaterfallLayout.m @@ -465,6 +465,27 @@ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { } } + if (self.enableStickyHeader) { + for (int i = 0; i < self.allItemAttributes.count; i++) { + UICollectionViewLayoutAttributes *attr = self.allItemAttributes[i]; + if ([attr.representedElementKind isEqualToString:CHTCollectionElementKindSectionHeader]) { + NSInteger section = attr.indexPath.section; + NSInteger numberOfItemsInSection = [self.collectionView numberOfItemsInSection:section]; + NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForItem:0 inSection:section]; + NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:MAX(0, numberOfItemsInSection - 1) inSection:section]; + UICollectionViewLayoutAttributes *firstCellAttributes = [self layoutAttributesForItemAtIndexPath:firstCellIndexPath]; + UICollectionViewLayoutAttributes *lastCellAttributes = [self layoutAttributesForItemAtIndexPath:lastCellIndexPath]; + CGFloat headerHeight = CGRectGetHeight(attr.frame); + CGPoint origin = attr.frame.origin; + CGFloat y1 = MAX(self.collectionView.contentOffset.y, (CGRectGetMinY(firstCellAttributes.frame) - headerHeight)); + CGFloat y2 = CGRectGetMaxY(lastCellAttributes.frame) - headerHeight; + origin.y = MIN(y1, y2); + attr.zIndex = 1024; + attr.frame = CGRectMake(origin.x, origin.y, CGRectGetWidth(attr.frame), CGRectGetHeight(attr.frame)); + supplHeaderAttrDict[attr.indexPath] = attr; + } + } + } NSArray *result = [cellAttrDict.allValues arrayByAddingObjectsFromArray:supplHeaderAttrDict.allValues]; result = [result arrayByAddingObjectsFromArray:supplFooterAttrDict.allValues]; result = [result arrayByAddingObjectsFromArray:decorAttrDict.allValues]; @@ -472,11 +493,14 @@ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { - CGRect oldBounds = self.collectionView.bounds; - if (CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds)) { - return YES; - } - return NO; + if (self.enableStickyHeader) { + return YES; + } + CGRect oldBounds = self.collectionView.bounds; + if (CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds)) { + return YES; + } + return NO; } #pragma mark - Private Methods diff --git a/Demo/Objective-C/Demo/ViewController.m b/Demo/Objective-C/Demo/ViewController.m index 6f87ea1..22afe0e 100644 --- a/Demo/Objective-C/Demo/ViewController.m +++ b/Demo/Objective-C/Demo/ViewController.m @@ -28,7 +28,7 @@ @implementation ViewController - (UICollectionView *)collectionView { if (!_collectionView) { CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init]; - + layout.enableStickyHeader = YES; layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); layout.headerHeight = 15; layout.footerHeight = 10; diff --git a/Demo/Objective-C/Demo/en.lproj/MainStoryboard_iPhone.storyboard b/Demo/Objective-C/Demo/en.lproj/MainStoryboard_iPhone.storyboard index f1a6f4e..170b809 100644 --- a/Demo/Objective-C/Demo/en.lproj/MainStoryboard_iPhone.storyboard +++ b/Demo/Objective-C/Demo/en.lproj/MainStoryboard_iPhone.storyboard @@ -1,8 +1,12 @@ - - + + + + + - - + + + @@ -10,9 +14,9 @@ - + - + @@ -20,9 +24,4 @@ - - - - - - \ No newline at end of file +