// // RAPhotoModel.m // Apex And Drivers // // Created by Jack on 2018/10/30. // Copyright © 2018年 USAI. All rights reserved. // #import "RAPhotoModel.h" #import "RAPhotoLayout.h" #import "RAPhotoItemModel.h" @interface RAPhotoModel () @property (nonatomic,strong) NSArray *layoutAttrs; @property (nonatomic,assign) CGFloat contentHeight; @end @implementation RAPhotoModel #pragma mark - Setter - (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues { [super setValuesForKeysWithDictionary:keyedValues]; } - (void)setPhotos:(NSArray *)photos { NSMutableArray *arr = [NSMutableArray array]; for (NSDictionary *photo in photos) { RAPhotoItemModel *model = [RAPhotoItemModel new]; [model setValuesForKeysWithDictionary:photo]; [arr addObject:model]; } _photos = [arr copy]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ [self prepareLayout]; }); } - (void)setDelegate:(id)delegate { if (_delegate && [_delegate respondsToSelector:@selector(unbind)]) { [_delegate unbind]; } _delegate = delegate; } #pragma mark - Getter - (CGFloat)height { return self.contentHeight + 36.5f; } - (CGFloat)width { if (_width <= 0) { self.width = [UIScreen mainScreen].bounds.size.width; } return _width; } - (CGFloat)contentHeight { if (_contentHeight <= 0) { [self prepareLayout]; } return _contentHeight; } - (RAPhotoItemModel *)photoItemAtIndex:(NSUInteger)index { if (index >= self.photos.count || index < 0) { return nil; } return [self.photos objectAtIndex:index]; } - (UIImage *)photoAtIndex:(NSUInteger)index { return [self photoItemAtIndex:index].photo; } - (NSUInteger)photoCount { __block NSUInteger count = 0; [self.photos enumerateObjectsUsingBlock:^(RAPhotoItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (!obj.isEmpty) { count++; } }]; return count; } - (NSArray *)photoArray { NSMutableArray *tmpArr = [NSMutableArray array]; [self.photos enumerateObjectsUsingBlock:^(RAPhotoItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (!obj.isEmpty) { [tmpArr addObject:obj.photo]; } }]; return [tmpArr copy]; } #pragma mark - Layout Delegate - (NSArray *)prepareLayout { NSUInteger itemCount = self.photos.count; if (itemCount == 0) { self.contentHeight = 0; return 0; } if (self.layoutAttrs) { return self.layoutAttrs; } NSMutableArray *attrs = [NSMutableArray arrayWithCapacity:itemCount]; CGFloat interval = 5.0f; NSUInteger colCount = 4; CGFloat width = (self.width - (interval * (colCount + 1))) / colCount; CGFloat height = width; for (int i = 0; i < itemCount; i++) { int col = i % colCount; int row = i / colCount; int x = interval + (interval + width) * col; int y = interval + (interval + height) * row; CGRect frame = CGRectMake(x, y, width, height); NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; UICollectionViewLayoutAttributes *itemAttrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; itemAttrs.frame = frame; [attrs addObject:itemAttrs]; self.contentHeight = CGRectGetMaxY(frame); } self.layoutAttrs = [attrs copy]; return self.layoutAttrs; } - (CGFloat)layoutContentHeight { return self.contentHeight; } @end