RAPhotoLayout.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // RAPhotoLayout.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/10/30.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAPhotoLayout.h"
  9. @interface RAPhotoLayout ()
  10. @property (nonatomic,strong) NSArray *attrs;
  11. @end
  12. @implementation RAPhotoLayout
  13. // 漏写了此方法,导致cell没显示。😓
  14. - (CGSize)collectionViewContentSize {
  15. return [self contentSize];
  16. }
  17. - (CGSize)contentSize {
  18. CGFloat height = 0;
  19. if (self.delegate) {
  20. height = [self.delegate layoutContentHeight];
  21. }
  22. CGSize size = CGSizeMake(self.collectionView.frame.size.width, height);
  23. return size;
  24. }
  25. - (void)prepareLayout {
  26. if (self.delegate) {
  27. self.attrs = [self.delegate prepareLayout];
  28. }
  29. }
  30. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  31. __block NSMutableArray *attrsArr = [NSMutableArray array];
  32. [self.attrs enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  33. if (CGRectIntersectsRect(rect, obj.frame)) {
  34. [attrsArr addObject:obj];
  35. }
  36. }];
  37. return attrsArr;
  38. }
  39. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
  40. UICollectionViewLayoutAttributes *itemtAttrs = [self.attrs objectAtIndex:indexPath.row];
  41. return itemtAttrs;
  42. }
  43. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
  44. CGRect oldBounds = self.collectionView.bounds;
  45. if (!CGSizeEqualToSize(newBounds.size, oldBounds.size)) {
  46. return YES;
  47. }
  48. return NO;
  49. }
  50. @end