RAPhotoModel.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. - (RAPhotoItemModel *)photoItemAtIndex:(NSUInteger)index {
  55. if (index >= self.photos.count || index < 0) {
  56. return nil;
  57. }
  58. return [self.photos objectAtIndex:index];
  59. }
  60. - (UIImage *)photoAtIndex:(NSUInteger)index {
  61. return [self photoItemAtIndex:index].photo;
  62. }
  63. - (NSUInteger)photoCount {
  64. __block NSUInteger count = 0;
  65. [self.photos enumerateObjectsUsingBlock:^(RAPhotoItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  66. if (!obj.isEmpty) {
  67. count++;
  68. }
  69. }];
  70. return count;
  71. }
  72. #pragma mark - Layout Delegate
  73. - (NSArray *)prepareLayout {
  74. NSUInteger itemCount = self.photos.count;
  75. if (itemCount == 0) {
  76. self.contentHeight = 0;
  77. return 0;
  78. }
  79. if (self.layoutAttrs) {
  80. return self.layoutAttrs;
  81. }
  82. NSMutableArray *attrs = [NSMutableArray arrayWithCapacity:itemCount];
  83. CGFloat interval = 5.0f;
  84. NSUInteger colCount = 4;
  85. CGFloat width = (self.width - (interval * (colCount + 1))) / colCount;
  86. CGFloat height = width;
  87. for (int i = 0; i < itemCount; i++) {
  88. int col = i % colCount;
  89. int row = i / colCount;
  90. int x = interval + (interval + width) * col;
  91. int y = interval + (interval + height) * row;
  92. CGRect frame = CGRectMake(x, y, width, height);
  93. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  94. UICollectionViewLayoutAttributes *itemAttrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  95. itemAttrs.frame = frame;
  96. [attrs addObject:itemAttrs];
  97. self.contentHeight = CGRectGetMaxY(frame);
  98. }
  99. self.layoutAttrs = [attrs copy];
  100. return self.layoutAttrs;
  101. }
  102. - (CGFloat)layoutContentHeight {
  103. return self.contentHeight;
  104. }
  105. @end