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

DRImageContainerView.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // DRImageContainerView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/28.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRImageContainerView.h"
  9. #import "DRContainerImageCell.h"
  10. #import "SDPhotoBrowser.h"
  11. @interface DRImageContainerView ()
  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 DRImageContainerView
  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:[DRContainerImageCell 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. DRShareImageCellModel *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. for (int i = 0; i<self.dataArr.count; i++) {
  102. DRShareImageCellModel *model = self.dataArr[i];
  103. if (i == index) {
  104. model.isSelected = YES;
  105. }else {
  106. model.isSelected = NO;
  107. }
  108. }
  109. [self reloadWithDataArray:self.dataArr];
  110. }
  111. - (void)tapImgAction:(UITapGestureRecognizer *)tap {
  112. UIImageView *imgView = (UIImageView *)tap.view;
  113. NSInteger index = imgView.tag - 10000;
  114. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  115. browser.sourceImagesContainerView = self.scrollView;
  116. browser.currentImageIndex = index;
  117. browser.delegate = self;
  118. browser.imageCount = self.dataArr.count;
  119. [browser show];
  120. }
  121. //#pragma mark ======
  122. //- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  123. // return self.dataArr.count;
  124. //}
  125. //
  126. //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  127. // return CGSizeMake(147, 147);
  128. //}
  129. //
  130. //
  131. //
  132. //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  133. // return UIEdgeInsetsMake(0, 15, 0, 5);
  134. //}
  135. //
  136. //- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  137. //
  138. // DRContainerImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  139. // DRShareImageCellModel *model = self.dataArr[indexPath.row];
  140. // cell.selectBlock = ^{
  141. // for (int i = 0; i < self.dataArr.count; i++) {
  142. // DRShareImageCellModel *item = self.dataArr[i];
  143. // if (i == indexPath.row) {
  144. // item.isSelected = YES;
  145. // }else {
  146. // item.isSelected = NO;
  147. // }
  148. // }
  149. // [self.collectionView reloadData];
  150. // };
  151. // cell.model = model;
  152. // if (indexPath.row == 0) {
  153. // cell.qrImgLabel.hidden = NO;
  154. // }else {
  155. // cell.qrImgLabel.hidden = YES;
  156. // }
  157. // return cell;
  158. //}
  159. //
  160. //- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  161. // SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  162. // browser.sourceImagesContainerView = self.collectionView;
  163. // browser.currentImageIndex = indexPath.row;
  164. // browser.delegate = self;
  165. // browser.imageCount = self.dataArr.count;
  166. // [browser show];
  167. //
  168. //}
  169. #pragma mark ----SDPhotoBrowser
  170. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
  171. UIImageView *imgView = self.imgViewArray[index];
  172. if (imgView.image) {
  173. return imgView.image;
  174. }
  175. return [UIImage imageNamed:@"placeHolder"];
  176. }
  177. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
  178. DRShareImageCellModel *model = self.dataArr[index];
  179. NSString *imgUrl = model.imgUrl;
  180. return [NSURL URLWithString:imgUrl];
  181. }
  182. #pragma mark ----
  183. - (NSMutableArray *)imgViewArray {
  184. if (!_imgViewArray) {
  185. _imgViewArray = [NSMutableArray array];
  186. }
  187. return _imgViewArray;
  188. }
  189. @end