RAPhotoModel.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // RAPhotoModel.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/10/30.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAPhotoModel.h"
  9. #import "RAPhotoLayout.h"
  10. #import "RAPhotoItemModel.h"
  11. @interface RAPhotoModel ()
  12. @property (nonatomic,strong) NSArray *layoutAttrs;
  13. @property (nonatomic,assign) CGFloat contentHeight;
  14. @end
  15. @implementation RAPhotoModel
  16. #pragma mark - Setter
  17. - (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValues {
  18. [super setValuesForKeysWithDictionary:keyedValues];
  19. }
  20. - (void)setPhotos:(NSArray *)photos {
  21. NSMutableArray *arr = [NSMutableArray array];
  22. for (NSDictionary *photo in photos) {
  23. RAPhotoItemModel *model = [RAPhotoItemModel new];
  24. [model setValuesForKeysWithDictionary:photo];
  25. [arr addObject:model];
  26. }
  27. _photos = [arr copy];
  28. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  29. [self prepareLayout];
  30. });
  31. }
  32. - (void)setDelegate:(id<RAPhotoModelDelegate>)delegate {
  33. if (_delegate && [_delegate respondsToSelector:@selector(unbind)]) {
  34. [_delegate unbind];
  35. }
  36. _delegate = delegate;
  37. }
  38. #pragma mark - Getter
  39. - (CGFloat)height {
  40. return self.contentHeight + 36.5f;
  41. }
  42. - (CGFloat)width {
  43. if (_width <= 0) {
  44. self.width = [UIScreen mainScreen].bounds.size.width;
  45. }
  46. return _width;
  47. }
  48. - (CGFloat)contentHeight {
  49. if (_contentHeight <= 0) {
  50. [self prepareLayout];
  51. }
  52. return _contentHeight;
  53. }
  54. #pragma mark - Layout Delegate
  55. - (NSArray *)prepareLayout {
  56. NSUInteger itemCount = self.photos.count;
  57. if (itemCount == 0) {
  58. self.contentHeight = 0;
  59. return 0;
  60. }
  61. if (self.layoutAttrs) {
  62. return self.layoutAttrs;
  63. }
  64. NSMutableArray *attrs = [NSMutableArray arrayWithCapacity:itemCount];
  65. CGFloat interval = 5.0f;
  66. NSUInteger colCount = 4;
  67. CGFloat width = (self.width - (interval * (colCount + 1))) / colCount;
  68. CGFloat height = width;
  69. for (int i = 0; i < itemCount; i++) {
  70. int col = i % colCount;
  71. int row = i / colCount;
  72. int x = interval + (interval + width) * col;
  73. int y = interval + (interval + height) * row;
  74. CGRect frame = CGRectMake(x, y, width, height);
  75. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  76. UICollectionViewLayoutAttributes *itemAttrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  77. itemAttrs.frame = frame;
  78. [attrs addObject:itemAttrs];
  79. self.contentHeight = CGRectGetMaxY(frame);
  80. }
  81. self.layoutAttrs = [attrs copy];
  82. return self.layoutAttrs;
  83. }
  84. - (CGFloat)layoutContentHeight {
  85. return self.contentHeight;
  86. }
  87. @end