|
| 1 | +/* |
| 2 | + * This file is part of the Canvas package. |
| 3 | + |
| 4 | + * |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + */ |
| 8 | + |
| 9 | +#import "CSStickyHeaderFlowLayout.h" |
| 10 | + |
| 11 | +NSString *const CSStickyHeaderParallexHeader = @"CSStickyHeaderParallexHeader"; |
| 12 | + |
| 13 | +@implementation CSStickyHeaderFlowLayout |
| 14 | + |
| 15 | +- (void)prepareLayout { |
| 16 | + [super prepareLayout]; |
| 17 | +} |
| 18 | + |
| 19 | +- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect |
| 20 | +{ |
| 21 | + // The rect should compensate the header size |
| 22 | + CGRect adjustedRect = rect; |
| 23 | + adjustedRect.origin.y -= self.parallexHeaderReferenceSize.height; |
| 24 | + |
| 25 | + NSMutableArray *allItems = [[super layoutAttributesForElementsInRect:adjustedRect] mutableCopy]; |
| 26 | + |
| 27 | + NSMutableDictionary *headers = [[NSMutableDictionary alloc] init]; |
| 28 | + NSMutableDictionary *lastCells = [[NSMutableDictionary alloc] init]; |
| 29 | + __block BOOL visibleParallexHeader; |
| 30 | + |
| 31 | + [allItems enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { |
| 32 | + UICollectionViewLayoutAttributes *attributes = obj; |
| 33 | + |
| 34 | + CGRect frame = attributes.frame; |
| 35 | + frame.origin.y += self.parallexHeaderReferenceSize.height; |
| 36 | + attributes.frame = frame; |
| 37 | + |
| 38 | + NSIndexPath *indexPath = [(UICollectionViewLayoutAttributes *)obj indexPath]; |
| 39 | + if ([[obj representedElementKind] isEqualToString:UICollectionElementKindSectionHeader]) { |
| 40 | + [headers setObject:obj forKey:@(indexPath.section)]; |
| 41 | + } else if ([[obj representedElementKind] isEqualToString:UICollectionElementKindSectionFooter]) { |
| 42 | + // Not implemeneted |
| 43 | + } else { |
| 44 | + NSIndexPath *indexPath = [(UICollectionViewLayoutAttributes *)obj indexPath]; |
| 45 | + |
| 46 | + UICollectionViewLayoutAttributes *currentAttribute = [lastCells objectForKey:@(indexPath.section)]; |
| 47 | + |
| 48 | + // Get the bottom most cell of that section |
| 49 | + if ( ! currentAttribute || indexPath.row > currentAttribute.indexPath.row) { |
| 50 | + [lastCells setObject:obj forKey:@(indexPath.section)]; |
| 51 | + } |
| 52 | + |
| 53 | + if ([indexPath item] == 0 && [indexPath section] == 0) { |
| 54 | + visibleParallexHeader = YES; |
| 55 | + } |
| 56 | + } |
| 57 | + }]; |
| 58 | + |
| 59 | + NSUInteger numberOfSections = [self.collectionView.dataSource numberOfSectionsInCollectionView:self.collectionView]; |
| 60 | + |
| 61 | + // Create the attributes for the Parallex header |
| 62 | + if (visibleParallexHeader && ! CGSizeEqualToSize(CGSizeZero, self.parallexHeaderReferenceSize) && numberOfSections > 0) { |
| 63 | + UICollectionViewLayoutAttributes *currentAttribute = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:CSStickyHeaderParallexHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 64 | + CGRect frame = currentAttribute.frame; |
| 65 | + frame.size.width = self.parallexHeaderReferenceSize.width; |
| 66 | + frame.size.height = self.parallexHeaderReferenceSize.height; |
| 67 | + |
| 68 | + CGRect bounds = self.collectionView.bounds; |
| 69 | + CGFloat maxY = CGRectGetMaxY(frame); |
| 70 | + |
| 71 | + // make sure the frame won't be negative values |
| 72 | + CGFloat y = MIN(maxY - self.parallexHeaderMinimumReferenceSize.height, bounds.origin.y + self.collectionView.contentInset.top); |
| 73 | + CGFloat height = MAX(0, -y + maxY); |
| 74 | + |
| 75 | + currentAttribute.frame = (CGRect){ |
| 76 | + frame.origin.x, |
| 77 | + y, |
| 78 | + frame.size.width, |
| 79 | + height, |
| 80 | + }; |
| 81 | + |
| 82 | + currentAttribute.zIndex = -1; |
| 83 | + |
| 84 | + [allItems addObject:currentAttribute]; |
| 85 | + } |
| 86 | + |
| 87 | + if ( ! self.disableStickyHeaders) { |
| 88 | + [lastCells enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { |
| 89 | + NSIndexPath *indexPath = [obj indexPath]; |
| 90 | + NSNumber *indexPathKey = @(indexPath.section); |
| 91 | + |
| 92 | + UICollectionViewLayoutAttributes *header = headers[indexPathKey]; |
| 93 | + // CollectionView automatically removes headers not in bounds |
| 94 | + if ( ! header) { |
| 95 | + header = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader |
| 96 | + atIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.section]]; |
| 97 | + |
| 98 | + if (header) { |
| 99 | + [allItems addObject:header]; |
| 100 | + } |
| 101 | + } |
| 102 | + [self updateHeaderAttributes:header lastCellAttributes:lastCells[indexPathKey]]; |
| 103 | + }]; |
| 104 | + } |
| 105 | + |
| 106 | + return allItems; |
| 107 | +} |
| 108 | + |
| 109 | +- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { |
| 110 | + UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath]; |
| 111 | + CGRect frame = attributes.frame; |
| 112 | + frame.origin.y += self.parallexHeaderReferenceSize.height; |
| 113 | + attributes.frame = frame; |
| 114 | + return attributes; |
| 115 | +} |
| 116 | + |
| 117 | +- (CGSize)collectionViewContentSize { |
| 118 | + CGSize size = [super collectionViewContentSize]; |
| 119 | + size.height += self.parallexHeaderReferenceSize.height; |
| 120 | + return size; |
| 121 | +} |
| 122 | + |
| 123 | +- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds |
| 124 | +{ |
| 125 | + return YES; |
| 126 | +} |
| 127 | + |
| 128 | +#pragma mark Helper |
| 129 | + |
| 130 | +- (void)updateHeaderAttributes:(UICollectionViewLayoutAttributes *)attributes lastCellAttributes:(UICollectionViewLayoutAttributes *)lastCellAttributes |
| 131 | +{ |
| 132 | + CGRect currentBounds = self.collectionView.bounds; |
| 133 | + attributes.zIndex = 1024; |
| 134 | + attributes.hidden = NO; |
| 135 | + |
| 136 | + CGPoint origin = attributes.frame.origin; |
| 137 | + |
| 138 | + CGFloat sectionMaxY = CGRectGetMaxY(lastCellAttributes.frame) - attributes.frame.size.height; |
| 139 | + CGFloat y = CGRectGetMaxY(currentBounds) - currentBounds.size.height + self.collectionView.contentInset.top; |
| 140 | + |
| 141 | + CGFloat maxY = MIN(MAX(y, attributes.frame.origin.y), sectionMaxY); |
| 142 | + |
| 143 | +// NSLog(@"%.2f, %.2f, %.2f", y, maxY, sectionMaxY); |
| 144 | + |
| 145 | + origin.y = maxY; |
| 146 | + |
| 147 | + attributes.frame = (CGRect){ |
| 148 | + origin, |
| 149 | + attributes.frame.size |
| 150 | + }; |
| 151 | +} |
| 152 | + |
| 153 | +@end |