猎豆优选

LDImageContainerView.m 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // LDImageContainerView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/28.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDImageContainerView.h"
  9. #import "LDContainerImageCell.h"
  10. #import "SDPhotoBrowser.h"
  11. @interface LDImageContainerView ()
  12. <
  13. UICollectionViewDelegate,
  14. UICollectionViewDataSource,
  15. UICollectionViewDelegateFlowLayout,
  16. UIScrollViewDelegate,
  17. SDPhotoBrowserDelegate
  18. >
  19. @property (nonatomic, strong) UICollectionView *collectionView;
  20. @property (nonatomic, strong) UIScrollView *scrollView;
  21. @property (nonatomic, strong) NSArray *dataArr;
  22. @property (nonatomic, strong) NSMutableArray *imgViewArray;
  23. @end
  24. static NSString *cellID = @"cellID";
  25. @implementation LDImageContainerView
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. if (self) {
  30. [self initSubViews];
  31. }
  32. return self;
  33. }
  34. - (void)initSubViews {
  35. // UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  36. // flowLayout.minimumLineSpacing = 5;
  37. // flowLayout.minimumInteritemSpacing = 5;
  38. // flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  39. // self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, 150) collectionViewLayout:flowLayout];
  40. // self.collectionView.delegate = self;
  41. // self.collectionView.dataSource = self;
  42. // self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xFDF8F1];
  43. // self.collectionView.showsVerticalScrollIndicator = NO;
  44. // self.collectionView.showsHorizontalScrollIndicator = NO;
  45. // [self.collectionView registerClass:[LDContainerImageCell class] forCellWithReuseIdentifier:cellID];
  46. // [self addSubview:self.collectionView];
  47. self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
  48. [self addSubview:self.scrollView];
  49. }
  50. - (void)reloadWithDataArray:(NSArray *)dataArr {
  51. self.dataArr = dataArr;
  52. CGFloat width = 147;
  53. [self.scrollView removeAllSubviews];
  54. [self.imgViewArray removeAllObjects];
  55. UIImageView *lastImgView;
  56. for (int i = 0; i<self.dataArr.count; i++) {
  57. LDShareImageCellModel *model = self.dataArr[i];
  58. UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(15+(width+5)*i, 2, width, width)];
  59. imgView.contentMode = UIViewContentModeScaleAspectFit;
  60. imgView.backgroundColor = [UIColor whiteColor];
  61. imgView.userInteractionEnabled = YES;
  62. imgView.tag = 10000+i;
  63. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgAction:)];
  64. [imgView addGestureRecognizer:tap];
  65. [self.imgViewArray addObject:imgView];
  66. UIButton *selBtn = [[UIButton alloc] initWithFrame:CGRectMake(width-40, 0, 40, 40)];
  67. [selBtn setImage:[UIImage imageNamed:@"cell_sel_n"] forState:UIControlStateNormal];
  68. [selBtn setImage:[UIImage imageNamed:@"cell_sel_s"] forState:UIControlStateSelected];
  69. selBtn.selected = model.isSelected;
  70. [selBtn addTarget:self action:@selector(selectedAction:) forControlEvents:UIControlEventTouchUpInside];
  71. selBtn.tag = 1000+i;
  72. [imgView addSubview:selBtn];
  73. [self.scrollView addSubview:imgView];
  74. if (i == 0) {
  75. if (!model.shareImg) {
  76. UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
  77. indicatorView.frame= CGRectMake(0, 0, 50, 50);
  78. indicatorView.color = [UIColor homeRedColor];
  79. indicatorView.center = CGPointMake(imgView.width/2, imgView.height/2);
  80. [indicatorView startAnimating];
  81. [imgView addSubview:indicatorView];
  82. }
  83. imgView.image = model.shareImg;
  84. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, width-28, width, 28)];
  85. label.backgroundColor = [UIColor YHColorWithHex:0xFF891A];
  86. label.textAlignment = NSTextAlignmentCenter;
  87. label.textColor = [UIColor whiteColor];
  88. label.font = [UIFont systemFontOfSize:13];
  89. label.text = @"二维码推广图";
  90. [imgView addSubview:label];
  91. }else {
  92. [imgView sd_setFadeImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:nil options:0 progress:nil completed:nil];
  93. }
  94. lastImgView = imgView;
  95. }
  96. // [self.collectionView reloadData];
  97. self.scrollView.contentSize = CGSizeMake(lastImgView.right+15, 150);
  98. }
  99. - (void)selectedAction:(UIButton *)sender {
  100. NSInteger index = sender.tag - 1000;
  101. LDShareImageCellModel *model = self.dataArr[index];
  102. model.isSelected = !model.isSelected;
  103. [self reloadWithDataArray:self.dataArr];
  104. }
  105. - (void)tapImgAction:(UITapGestureRecognizer *)tap {
  106. UIImageView *imgView = (UIImageView *)tap.view;
  107. NSInteger index = imgView.tag - 10000;
  108. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  109. browser.sourceImagesContainerView = self.scrollView;
  110. browser.currentImageIndex = index;
  111. browser.delegate = self;
  112. browser.imageCount = self.dataArr.count;
  113. [browser show];
  114. }
  115. //#pragma mark ======
  116. //- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  117. // return self.dataArr.count;
  118. //}
  119. //
  120. //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  121. // return CGSizeMake(147, 147);
  122. //}
  123. //
  124. //
  125. //
  126. //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  127. // return UIEdgeInsetsMake(0, 15, 0, 5);
  128. //}
  129. //
  130. //- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  131. //
  132. // LDContainerImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  133. // LDShareImageCellModel *model = self.dataArr[indexPath.row];
  134. // cell.selectBlock = ^{
  135. // for (int i = 0; i < self.dataArr.count; i++) {
  136. // LDShareImageCellModel *item = self.dataArr[i];
  137. // if (i == indexPath.row) {
  138. // item.isSelected = YES;
  139. // }else {
  140. // item.isSelected = NO;
  141. // }
  142. // }
  143. // [self.collectionView reloadData];
  144. // };
  145. // cell.model = model;
  146. // if (indexPath.row == 0) {
  147. // cell.qrImgLabel.hidden = NO;
  148. // }else {
  149. // cell.qrImgLabel.hidden = YES;
  150. // }
  151. // return cell;
  152. //}
  153. //
  154. //- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  155. // SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  156. // browser.sourceImagesContainerView = self.collectionView;
  157. // browser.currentImageIndex = indexPath.row;
  158. // browser.delegate = self;
  159. // browser.imageCount = self.dataArr.count;
  160. // [browser show];
  161. //
  162. //}
  163. #pragma mark ----SDPhotoBrowser
  164. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
  165. UIImageView *imgView = self.imgViewArray[index];
  166. if (imgView.image) {
  167. return imgView.image;
  168. }
  169. return [UIImage imageNamed:@"placeHolder"];
  170. }
  171. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
  172. LDShareImageCellModel *model = self.dataArr[index];
  173. NSString *imgUrl = model.imgUrl;
  174. return [NSURL URLWithString:imgUrl];
  175. }
  176. #pragma mark ----
  177. - (NSMutableArray *)imgViewArray {
  178. if (!_imgViewArray) {
  179. _imgViewArray = [NSMutableArray array];
  180. }
  181. return _imgViewArray;
  182. }
  183. @end