| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- //
- // 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<NSString *,id> *)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<RAPhotoModelDelegate>)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;
- }
- #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
|