// // MineListMindImgView.m // 乐销 // // Created by 隋林栋 on 2017/10/23. //Copyright © 2017年 ping. All rights reserved. // #import "MineListMindImgView.h" //detail big image view #import "ImageDetailBigView.h" @interface MineListMindImgView() @property (nonatomic, assign) CGFloat imgWidth; @property (nonatomic, assign) CGFloat imgHeight; @property (nonatomic, strong) UILabel *labelNum; @end @implementation MineListMindImgView #pragma mark lazy init - (NSMutableDictionary *)dicImages{ if (!_dicImages) { _dicImages = [NSMutableDictionary new]; } return _dicImages; } - (NSArray *)aryModels{ if (!_aryModels) { _aryModels = [NSArray new]; } return _aryModels; } - (UILabel *)labelNum{ if (!_labelNum) { _labelNum = [UILabel new]; _labelNum.fontNum = F(23); _labelNum.textColor = [UIColor whiteColor]; _labelNum.textAlignment = NSTextAlignmentCenter; _labelNum.userInteractionEnabled = true; _labelNum.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5]; _labelNum.hidden = true; } return _labelNum; } - (UIImageView *)imageWithIndex:(NSInteger )index{ UIImageView * iv = [self.dicImages objectForKey:[NSString stringWithFormat:@"%ld",index]]; if (!iv) { iv = [UIImageView new]; iv.contentMode = UIViewContentModeScaleAspectFill; iv.clipsToBounds = true; [self.dicImages setObject:iv forKey:[NSString stringWithFormat:@"%ld",index]]; iv.tag = index; iv.userInteractionEnabled = true; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageClick:)]; [iv addGestureRecognizer:tap]; } return iv; } - (CGFloat)imgWidth{ switch (self.aryModels.count) { case 0: return 0; break; case 1: return W(135); break; case 2: case 4: return W(103); break; default: return W(103); } return 0; } - (CGFloat)imgHeight{ switch (self.aryModels.count) { case 0: return 0; break; case 1: return W(240); break; case 2: case 4: return W(103); break; default: return W(103); break; } return 0; } #pragma mark init - (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.imgSpace = W(9); self.leftInteral = W(12); self.topInteral = W(0); self.width = SCREEN_WIDTH; self.numLimit = 9; } return self; } #pragma mark 刷新view - (void)resetViewWithArray:(NSArray *)array{ [self resetViewWithArray:array totalNum:array.count]; } - (void)resetViewWithArray:(NSArray *)array totalNum:(double)totalNum{ [self removeSubViewWithTag:TAG_LINE]; self.aryModels = array; //刷新view CGFloat top = self.topInteral; CGFloat left = self.leftInteral; self.height = 0; self.labelNum.hidden = true; NSInteger numItems = totalNum == 4?4:MIN(totalNum, self.numLimit); for (int i = 0; i < numItems; i++) { ModelImage *model = array[i]; UIImageView *img = [self imageWithIndex:i]; img.frame = CGRectMake(left, top, self.imgWidth, self.imgHeight); [img sd_setImageWithURL:model.url==nil?nil:[NSURL URLWithString:(i==0 && totalNum == 1)?model.url:model.url] placeholderImage:[UIImage imageNamed:IMAGE_BIG_DEFAULT]]; img.contentMode = UIViewContentModeScaleAspectFill; img.tag = TAG_LINE; [self addSubview:img]; left = img.right + self.imgSpace; if (totalNum == 2){ left = img.right + self.imgSpace; }else if (totalNum == 4 ) { if (i == 1) { left = self.leftInteral; top = img.bottom + self.imgSpace; } }else if ((i+1)%3 == 0 ){ left = self.leftInteral; top = img.bottom + self.imgSpace; }else if (totalNum ==1){ img.centerX = SCREEN_WIDTH/2; } self.height = img.bottom + self.bottomInterval; //show label num if (i == self.numLimit - 1 && totalNum > self.numLimit && totalNum != 4) { self.labelNum.text = [NSString stringWithFormat:@"+%ld",(NSInteger)totalNum - self.numLimit]; self.labelNum.widthHeight = img.widthHeight; [img addSubview:self.labelNum]; self.labelNum.hidden = false; } } } #pragma mark image Click - (void)imageClick:(UITapGestureRecognizer *)tap{ UIImageView * view = (UIImageView *)tap.view; ImageDetailBigView * detailView = [ImageDetailBigView new]; [detailView resetView:self.aryModels.tmpMuAry isEdit:false index: view.tag]; [detailView showInView:GB_Nav.lastVC.view imageViewShow:view]; } @end