|
| 1 | +// |
| 2 | +// MTZCollectionViewFlowLayout.m |
| 3 | +// MTZCollectionViewFlowLayout |
| 4 | +// |
| 5 | +// Created by Matt Zanchelli on 5/29/14. |
| 6 | +// Copyright (c) 2014 Matt Zanchelli. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +// With some left alignment help: https://github.com/mokagio/UICollectionViewLeftAlignedLayout |
| 10 | +// |
| 11 | +// Copyright (c) 2014 Giovanni Lodi |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 14 | +// this software and associated documentation files (the "Software"), to deal in |
| 15 | +// the Software without restriction, including without limitation the rights to |
| 16 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 17 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 18 | +// subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 25 | +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 26 | +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 27 | +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 28 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 29 | + |
| 30 | +#import "MTZCollectionViewFlowLayout.h" |
| 31 | + |
| 32 | +@implementation MTZCollectionViewFlowLayout |
| 33 | + |
| 34 | +- (id)init |
| 35 | +{ |
| 36 | + self = [super init]; |
| 37 | + if (self) { |
| 38 | + self.treatSizeAsMinimumSize = YES; |
| 39 | + } |
| 40 | + return self; |
| 41 | +} |
| 42 | + |
| 43 | +- (id)initWithCoder:(NSCoder *)aDecoder |
| 44 | +{ |
| 45 | + self = [super initWithCoder:aDecoder]; |
| 46 | + if (self) { |
| 47 | + self.treatSizeAsMinimumSize = YES; |
| 48 | + } |
| 49 | + return self; |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +#pragma mark - Properties |
| 54 | + |
| 55 | +- (void)setTreatSizeAsMinimumSize:(BOOL)treatSizeAsMinimumSize |
| 56 | +{ |
| 57 | + _treatSizeAsMinimumSize = treatSizeAsMinimumSize; |
| 58 | + |
| 59 | + UICollectionViewFlowLayoutInvalidationContext *ctx = [[UICollectionViewFlowLayoutInvalidationContext alloc] init]; |
| 60 | + ctx.invalidateFlowLayoutDelegateMetrics = YES; |
| 61 | + [self invalidateLayoutWithContext:ctx]; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +#pragma mark - Providing Layout Attributes |
| 66 | + |
| 67 | +- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect |
| 68 | +{ |
| 69 | + NSArray *allAttributes = [super layoutAttributesForElementsInRect:rect]; |
| 70 | + for ( UICollectionViewLayoutAttributes *attributes in allAttributes ) { |
| 71 | + if ( !attributes.representedElementKind ) { |
| 72 | + NSIndexPath *indexPath = attributes.indexPath; |
| 73 | + attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame; |
| 74 | + } |
| 75 | + } |
| 76 | + return allAttributes; |
| 77 | +} |
| 78 | + |
| 79 | +- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath |
| 80 | +{ |
| 81 | + UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath]; |
| 82 | + |
| 83 | + // Return early if not treating as minimum size. |
| 84 | + if ( !self.treatsSizeAsMinimumSize ) return attributes; |
| 85 | + |
| 86 | + // Get some basic measurements. |
| 87 | + UIEdgeInsets sectionInset; |
| 88 | + if ( [self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)] ) { |
| 89 | + [((id<UICollectionViewDelegateFlowLayout>)self.collectionView.delegate) collectionView:self.collectionView layout:self insetForSectionAtIndex:indexPath.section]; |
| 90 | + } else { |
| 91 | + sectionInset = self.sectionInset; |
| 92 | + } |
| 93 | + |
| 94 | + CGFloat interItemSpacing; |
| 95 | + if ( [self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)] ) { |
| 96 | + interItemSpacing = [((id<UICollectionViewDelegateFlowLayout>)self.collectionView.delegate) collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:indexPath.section]; |
| 97 | + } else { |
| 98 | + interItemSpacing = self.minimumInteritemSpacing; |
| 99 | + } |
| 100 | + |
| 101 | + // Measurements dependent on scroll direction. |
| 102 | + CGFloat totalDimension, totalSectionInset, minimumItemDimension; |
| 103 | + switch ( self.scrollDirection ) { |
| 104 | + case UICollectionViewScrollDirectionVertical: |
| 105 | + totalDimension = self.collectionView.bounds.size.width; |
| 106 | + totalSectionInset = sectionInset.left + sectionInset.right; |
| 107 | + minimumItemDimension = self.itemSize.width; |
| 108 | + break; |
| 109 | + case UICollectionViewScrollDirectionHorizontal: |
| 110 | + totalDimension = self.collectionView.bounds.size.height; |
| 111 | + totalSectionInset = sectionInset.top + sectionInset.bottom; |
| 112 | + minimumItemDimension = self.itemSize.height; |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + // Calculate the new dimension based on working dimension and number of items in a line. |
| 117 | + CGFloat workingDimension = totalDimension - totalSectionInset; |
| 118 | + CGFloat numberOfItemsInLine = floor((workingDimension - interItemSpacing) / (minimumItemDimension + interItemSpacing)); |
| 119 | + CGFloat newDimension = MAX(minimumItemDimension, workingDimension / numberOfItemsInLine); |
| 120 | + |
| 121 | + // Set the new size. |
| 122 | + switch ( self.scrollDirection ) { |
| 123 | + case UICollectionViewScrollDirectionVertical: |
| 124 | + attributes.size = CGSizeMake(newDimension, attributes.size.height); |
| 125 | + break; |
| 126 | + case UICollectionViewScrollDirectionHorizontal: |
| 127 | + attributes.size = CGSizeMake(attributes.size.width, newDimension); |
| 128 | + break; |
| 129 | + } |
| 130 | + |
| 131 | + /* |
| 132 | + // Align to the left. |
| 133 | + if ( indexPath.item == 0 ) { |
| 134 | + CGRect frame = attributes.frame; |
| 135 | + frame.origin.x = 0; |
| 136 | + attributes.frame = frame; |
| 137 | + } else { |
| 138 | + NSIndexPath *previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section]; |
| 139 | + CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame; |
| 140 | + CGRect strecthedCurrentFrame = CGRectMake(0, attributes.frame.origin.y, workingDimension, attributes.frame.size.height); |
| 141 | + |
| 142 | + // If the current frame, once aligned to the left and stretched to the full collection view width, intersects the previous frame, then they are on the same line. |
| 143 | + if ( !CGRectIntersectsRect(previousFrame, strecthedCurrentFrame) ) { |
| 144 | + // Make sure the first item on a line is left aligned. |
| 145 | + CGRect frame = attributes.frame; |
| 146 | + frame.origin.x = 0; |
| 147 | + attributes.frame = frame; |
| 148 | + } else { |
| 149 | + attributes.frame = CGRectMake(CGRectGetMaxX(previousFrame) + interItemSpacing, attributes.frame.origin.y, attributes.frame.size.width, attributes.frame.size.height); |
| 150 | + } |
| 151 | + } |
| 152 | + */ |
| 153 | + |
| 154 | + return attributes; |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | +#pragma mark - Invalidating the Layout |
| 159 | + |
| 160 | +- (UICollectionViewLayoutInvalidationContext *)invalidationContextForBoundsChange:(CGRect)newBounds |
| 161 | +{ |
| 162 | + // This cast should hopefully never cause issues. `super` should always return `UICollectionViewFlowLayoutInvalidationContext`. |
| 163 | + UICollectionViewFlowLayoutInvalidationContext *ctx = (UICollectionViewFlowLayoutInvalidationContext *) [super invalidationContextForBoundsChange:newBounds]; |
| 164 | + ctx.invalidateFlowLayoutDelegateMetrics = YES; |
| 165 | + return ctx; |
| 166 | +} |
| 167 | + |
| 168 | +- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds |
| 169 | +{ |
| 170 | + /* |
| 171 | + // Only bother if this option is set. |
| 172 | + if ( self.treatSizeAsMinimumSize ) { |
| 173 | + // The new and old dimension (that we care about) |
| 174 | + CGFloat newDimension, oldDimension; |
| 175 | + // Set the new/old dimension according to scroll direction. |
| 176 | + switch (self.scrollDirection) { |
| 177 | + case UICollectionViewScrollDirectionVertical: |
| 178 | + newDimension = newBounds.size.width; |
| 179 | + oldDimension = self.collectionView.bounds.size.width; |
| 180 | + break; |
| 181 | + case UICollectionViewScrollDirectionHorizontal: |
| 182 | + newDimension = newBounds.size.height; |
| 183 | + oldDimension = self.collectionView.bounds.size.height; |
| 184 | + break; |
| 185 | + } |
| 186 | + |
| 187 | + // If the dimension we care about changed, invalidate. |
| 188 | + if ( newDimension != oldDimension ) { |
| 189 | + return YES; |
| 190 | + } |
| 191 | + } |
| 192 | + */ |
| 193 | + |
| 194 | + return [super shouldInvalidateLayoutForBoundsChange:newBounds]; |
| 195 | +} |
| 196 | + |
| 197 | +@end |
0 commit comments