Brak opisu

UICollectionViewLeftAlignedLayout.m 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2014 Giovanni Lodi
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. // this software and associated documentation files (the "Software"), to deal in
  5. // the Software without restriction, including without limitation the rights to
  6. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7. // the Software, and to permit persons to whom the Software is furnished to do so,
  8. // subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  15. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  17. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. #import "UICollectionViewLeftAlignedLayout.h"
  20. @interface UICollectionViewLayoutAttributes (LeftAligned)
  21. - (void)leftAlignFrameWithSectionInset:(UIEdgeInsets)sectionInset;
  22. @end
  23. @implementation UICollectionViewLayoutAttributes (LeftAligned)
  24. - (void)leftAlignFrameWithSectionInset:(UIEdgeInsets)sectionInset
  25. {
  26. CGRect frame = self.frame;
  27. frame.origin.x = sectionInset.left;
  28. self.frame = frame;
  29. }
  30. @end
  31. #pragma mark -
  32. @implementation UICollectionViewLeftAlignedLayout
  33. #pragma mark - UICollectionViewLayout
  34. - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
  35. NSArray *originalAttributes = [super layoutAttributesForElementsInRect:rect];
  36. NSMutableArray *updatedAttributes = [NSMutableArray arrayWithArray:originalAttributes];
  37. for (UICollectionViewLayoutAttributes *attributes in originalAttributes) {
  38. if (!attributes.representedElementKind) {
  39. NSUInteger index = [updatedAttributes indexOfObject:attributes];
  40. updatedAttributes[index] = [self layoutAttributesForItemAtIndexPath:attributes.indexPath];
  41. }
  42. }
  43. return updatedAttributes;
  44. }
  45. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
  46. UICollectionViewLayoutAttributes* currentItemAttributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];
  47. UIEdgeInsets sectionInset = [self evaluatedSectionInsetForItemAtIndex:indexPath.section];
  48. BOOL isFirstItemInSection = indexPath.item == 0;
  49. CGFloat layoutWidth = CGRectGetWidth(self.collectionView.frame) - sectionInset.left - sectionInset.right;
  50. if (isFirstItemInSection) {
  51. [currentItemAttributes leftAlignFrameWithSectionInset:sectionInset];
  52. return currentItemAttributes;
  53. }
  54. NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
  55. CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
  56. CGFloat previousFrameRightPoint = previousFrame.origin.x + previousFrame.size.width;
  57. CGRect currentFrame = currentItemAttributes.frame;
  58. CGRect strecthedCurrentFrame = CGRectMake(sectionInset.left,
  59. currentFrame.origin.y,
  60. layoutWidth,
  61. currentFrame.size.height);
  62. // if the current frame, once left aligned to the left and stretched to the full collection view
  63. // widht intersects the previous frame then they are on the same line
  64. BOOL isFirstItemInRow = !CGRectIntersectsRect(previousFrame, strecthedCurrentFrame);
  65. if (isFirstItemInRow) {
  66. // make sure the first item on a line is left aligned
  67. [currentItemAttributes leftAlignFrameWithSectionInset:sectionInset];
  68. return currentItemAttributes;
  69. }
  70. CGRect frame = currentItemAttributes.frame;
  71. frame.origin.x = previousFrameRightPoint + [self evaluatedMinimumInteritemSpacingForSectionAtIndex:indexPath.section];
  72. currentItemAttributes.frame = frame;
  73. return currentItemAttributes;
  74. }
  75. - (CGFloat)evaluatedMinimumInteritemSpacingForSectionAtIndex:(NSInteger)sectionIndex
  76. {
  77. if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
  78. id<UICollectionViewDelegateLeftAlignedLayout> delegate = (id<UICollectionViewDelegateLeftAlignedLayout>)self.collectionView.delegate;
  79. return [delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:sectionIndex];
  80. } else {
  81. return self.minimumInteritemSpacing;
  82. }
  83. }
  84. - (UIEdgeInsets)evaluatedSectionInsetForItemAtIndex:(NSInteger)index
  85. {
  86. if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
  87. id<UICollectionViewDelegateLeftAlignedLayout> delegate = (id<UICollectionViewDelegateLeftAlignedLayout>)self.collectionView.delegate;
  88. return [delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:index];
  89. } else {
  90. return self.sectionInset;
  91. }
  92. }
  93. @end