// // PhotoContainerView.m // YouHuiProject // // Created by 小花 on 2018/5/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "PhotoContainerView.h" #import "SDPhotoBrowser.h" #import "UIView+SDAutoLayout.h" @interface PhotoContainerView () @property (nonatomic, strong) NSArray *imageViewsArray; @end @implementation PhotoContainerView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setup]; } return self; } - (void)setup { NSMutableArray *temp = [NSMutableArray new]; for (int i = 0; i < 9; i++) { UIImageView *imageView = [UIImageView new]; [self addSubview:imageView]; imageView.userInteractionEnabled = YES; imageView.tag = i; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]; [imageView addGestureRecognizer:tap]; UIImageView *bottomView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(60), 15)]; bottomView.image = [UIImage imageNamed:@"bottomImg"]; [imageView addSubview:bottomView]; UILabel *priceLb = [[UILabel alloc] initWithFrame:bottomView.bounds]; priceLb.textColor = [UIColor whiteColor]; priceLb.textAlignment = NSTextAlignmentCenter; priceLb.font = [UIFont systemFontOfSize:Fitsize(11)]; [bottomView addSubview:priceLb]; [temp addObject:imageView]; } self.imageViewsArray = [temp copy]; } - (void)setPicPathStringsArray:(NSArray *)picPathStringsArray { _picPathStringsArray = picPathStringsArray; for (long i = _picPathStringsArray.count; i < self.imageViewsArray.count; i++) { UIImageView *imageView = [self.imageViewsArray objectAtIndex:i]; imageView.hidden = YES; } if (_picPathStringsArray.count == 0) { self.height = 0; self.fixedHeight = @(0); return; } CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray]; CGFloat itemH = 0; // if (_picPathStringsArray.count == 1) { // UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject]; // if (image.size.width) { // itemH = image.size.height / image.size.width * itemW; // } // } else { // itemH = itemW; // } itemH = itemW; long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray]; CGFloat margin = 5; [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { long columnIndex = idx % perRowItemCount; long rowIndex = idx / perRowItemCount; UIImageView *imageView = [_imageViewsArray objectAtIndex:idx]; imageView.hidden = NO; [imageView yy_setImageWithURL:[NSURL URLWithString:obj] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation]; imageView.backgroundColor = [UIColor yhGrayColor]; imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH); UIImageView *bottomImg = imageView.subviews.firstObject; bottomImg.bottom = imageView.height; bottomImg.right = imageView.right; }]; CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin; int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount); CGFloat h = columnCount * itemH + (columnCount - 1) * margin; self.width = w; self.height = h; self.fixedHeight = @(h); self.fixedWidth = @(w); } - (void)setModelArray:(NSArray *)modelArray { _modelArray = modelArray; for (long i = _modelArray.count; i < self.imageViewsArray.count; i++) { UIImageView *imageView = [self.imageViewsArray objectAtIndex:i]; imageView.hidden = YES; } if (_modelArray.count == 0) { self.height = 0; self.fixedHeight = @(0); return; } CGFloat itemW = [self itemWidthForPicPathArray:_modelArray]; CGFloat itemH = 0; // if (_picPathStringsArray.count == 1) { // UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject]; // if (image.size.width) { // itemH = image.size.height / image.size.width * itemW; // } // } else { // itemH = itemW; // } itemH = itemW; long perRowItemCount = [self perRowItemCountForPicPathArray:_modelArray]; CGFloat margin = 5; [_modelArray enumerateObjectsUsingBlock:^(DRCommunityDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { long columnIndex = idx % perRowItemCount; long rowIndex = idx / perRowItemCount; UIImageView *imageView = [_imageViewsArray objectAtIndex:idx]; imageView.hidden = NO; [imageView yy_setImageWithURL:[NSURL URLWithString:obj.img] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation]; imageView.backgroundColor = [UIColor yhGrayColor]; imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH); UIImageView *bottomImg = imageView.subviews.firstObject; bottomImg.bottom = imageView.height; bottomImg.right = imageView.width; UILabel *priceLb = bottomImg.subviews.firstObject; priceLb.text = [NSString stringWithFormat:@"¥%.2f",[obj.discount_price floatValue]]; }]; CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin; int columnCount = ceilf(_modelArray.count * 1.0 / perRowItemCount); CGFloat h = columnCount * itemH + (columnCount - 1) * margin; self.width = w; self.height = h; self.fixedHeight = @(h); self.fixedWidth = @(w); } #pragma mark - private actions - (void)tapImageView:(UITapGestureRecognizer *)tap { UIView *imageView = tap.view; if (self.delegate &&[self.delegate respondsToSelector:@selector(otherOPByModel:)]) { [self.delegate otherOPByModel:self.modelArray[imageView.tag]]; }else{ SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init]; browser.currentImageIndex = imageView.tag; browser.sourceImagesContainerView = self; browser.imageCount = self.picPathStringsArray.count; browser.delegate = self; [browser show]; } } - (CGFloat)itemWidthForPicPathArray:(NSArray *)array { if (array.count == 1) { return 120; } else { CGFloat margin = 5; CGFloat w = ([UIScreen mainScreen].bounds.size.width-20-margin*2)/3; return w; } } - (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array { // if (array.count < 3) { // return array.count; // } else if (array.count <= 4) { // return 2; // } else { // return 3; // } if (array.count<=3) { return array.count; } return 3; } #pragma mark - SDPhotoBrowserDelegate - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index { NSString *imageName = self.picPathStringsArray[index]; NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil]; return url; } - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index { UIImageView *imageView = self.subviews[index]; return imageView.image; } @end