《省钱达人》与《猎豆优选》UI相同版。域名tbk

PhotoContainerView.m 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // PhotoContainerView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "PhotoContainerView.h"
  9. #import "SDPhotoBrowser.h"
  10. #import "UIView+SDAutoLayout.h"
  11. @interface PhotoContainerView ()<SDPhotoBrowserDelegate>
  12. @property (nonatomic, strong) NSArray *imageViewsArray;
  13. @end
  14. @implementation PhotoContainerView
  15. - (instancetype)initWithFrame:(CGRect)frame
  16. {
  17. if (self = [super initWithFrame:frame]) {
  18. [self setup];
  19. }
  20. return self;
  21. }
  22. - (void)setup
  23. {
  24. NSMutableArray *temp = [NSMutableArray new];
  25. for (int i = 0; i < 9; i++) {
  26. UIImageView *imageView = [UIImageView new];
  27. [self addSubview:imageView];
  28. imageView.userInteractionEnabled = YES;
  29. imageView.tag = i;
  30. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
  31. [imageView addGestureRecognizer:tap];
  32. UIImageView *bottomView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Fitsize(60), 15)];
  33. bottomView.image = [UIImage imageNamed:@"bottomImg"];
  34. [imageView addSubview:bottomView];
  35. UILabel *priceLb = [[UILabel alloc] initWithFrame:bottomView.bounds];
  36. priceLb.textColor = [UIColor whiteColor];
  37. priceLb.textAlignment = NSTextAlignmentCenter;
  38. priceLb.font = [UIFont systemFontOfSize:Fitsize(11)];
  39. [bottomView addSubview:priceLb];
  40. [temp addObject:imageView];
  41. }
  42. self.imageViewsArray = [temp copy];
  43. }
  44. - (void)setPicPathStringsArray:(NSArray *)picPathStringsArray
  45. {
  46. _picPathStringsArray = picPathStringsArray;
  47. for (long i = _picPathStringsArray.count; i < self.imageViewsArray.count; i++) {
  48. UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];
  49. imageView.hidden = YES;
  50. }
  51. if (_picPathStringsArray.count == 0) {
  52. self.height = 0;
  53. self.fixedHeight = @(0);
  54. return;
  55. }
  56. CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray];
  57. CGFloat itemH = 0;
  58. // if (_picPathStringsArray.count == 1) {
  59. // UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject];
  60. // if (image.size.width) {
  61. // itemH = image.size.height / image.size.width * itemW;
  62. // }
  63. // } else {
  64. // itemH = itemW;
  65. // }
  66. itemH = itemW;
  67. long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray];
  68. CGFloat margin = 5;
  69. [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  70. long columnIndex = idx % perRowItemCount;
  71. long rowIndex = idx / perRowItemCount;
  72. UIImageView *imageView = [_imageViewsArray objectAtIndex:idx];
  73. imageView.hidden = NO;
  74. [imageView yy_setImageWithURL:[NSURL URLWithString:obj] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
  75. imageView.backgroundColor = [UIColor yhGrayColor];
  76. imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
  77. UIImageView *bottomImg = imageView.subviews.firstObject;
  78. bottomImg.bottom = imageView.height;
  79. bottomImg.right = imageView.right;
  80. }];
  81. CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
  82. int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
  83. CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
  84. self.width = w;
  85. self.height = h;
  86. self.fixedHeight = @(h);
  87. self.fixedWidth = @(w);
  88. }
  89. - (void)setModelArray:(NSArray<DRCommunityDetailModel *> *)modelArray {
  90. _modelArray = modelArray;
  91. for (long i = _modelArray.count; i < self.imageViewsArray.count; i++) {
  92. UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];
  93. imageView.hidden = YES;
  94. }
  95. if (_modelArray.count == 0) {
  96. self.height = 0;
  97. self.fixedHeight = @(0);
  98. return;
  99. }
  100. CGFloat itemW = [self itemWidthForPicPathArray:_modelArray];
  101. CGFloat itemH = 0;
  102. // if (_picPathStringsArray.count == 1) {
  103. // UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject];
  104. // if (image.size.width) {
  105. // itemH = image.size.height / image.size.width * itemW;
  106. // }
  107. // } else {
  108. // itemH = itemW;
  109. // }
  110. itemH = itemW;
  111. long perRowItemCount = [self perRowItemCountForPicPathArray:_modelArray];
  112. CGFloat margin = 5;
  113. [_modelArray enumerateObjectsUsingBlock:^(DRCommunityDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  114. long columnIndex = idx % perRowItemCount;
  115. long rowIndex = idx / perRowItemCount;
  116. UIImageView *imageView = [_imageViewsArray objectAtIndex:idx];
  117. imageView.hidden = NO;
  118. [imageView yy_setImageWithURL:[NSURL URLWithString:obj.img] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
  119. imageView.backgroundColor = [UIColor yhGrayColor];
  120. imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
  121. UIImageView *bottomImg = imageView.subviews.firstObject;
  122. bottomImg.bottom = imageView.height;
  123. bottomImg.right = imageView.width;
  124. UILabel *priceLb = bottomImg.subviews.firstObject;
  125. priceLb.text = [NSString stringWithFormat:@"¥%.2f",[obj.discount_price floatValue]];
  126. }];
  127. CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
  128. int columnCount = ceilf(_modelArray.count * 1.0 / perRowItemCount);
  129. CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
  130. self.width = w;
  131. self.height = h;
  132. self.fixedHeight = @(h);
  133. self.fixedWidth = @(w);
  134. }
  135. #pragma mark - private actions
  136. - (void)tapImageView:(UITapGestureRecognizer *)tap
  137. {
  138. UIView *imageView = tap.view;
  139. if (self.delegate &&[self.delegate respondsToSelector:@selector(otherOPByModel:)]) {
  140. [self.delegate otherOPByModel:self.modelArray[imageView.tag]];
  141. }else{
  142. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  143. browser.currentImageIndex = imageView.tag;
  144. browser.sourceImagesContainerView = self;
  145. browser.imageCount = self.picPathStringsArray.count;
  146. browser.delegate = self;
  147. [browser show];
  148. }
  149. }
  150. - (CGFloat)itemWidthForPicPathArray:(NSArray *)array
  151. {
  152. if (array.count == 1) {
  153. return 120;
  154. } else {
  155. CGFloat margin = 5;
  156. CGFloat w = ([UIScreen mainScreen].bounds.size.width-20-margin*2)/3;
  157. return w;
  158. }
  159. }
  160. - (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
  161. {
  162. // if (array.count < 3) {
  163. // return array.count;
  164. // } else if (array.count <= 4) {
  165. // return 2;
  166. // } else {
  167. // return 3;
  168. // }
  169. if (array.count<=3) {
  170. return array.count;
  171. }
  172. return 3;
  173. }
  174. #pragma mark - SDPhotoBrowserDelegate
  175. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
  176. {
  177. NSString *imageName = self.picPathStringsArray[index];
  178. NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
  179. return url;
  180. }
  181. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
  182. {
  183. UIImageView *imageView = self.subviews[index];
  184. return imageView.image;
  185. }
  186. @end