123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- //
- // DRImageContainerView.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/12/28.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "DRImageContainerView.h"
- #import "DRContainerImageCell.h"
- #import "SDPhotoBrowser.h"
- @interface DRImageContainerView ()
- <
- UICollectionViewDelegate,
- UICollectionViewDataSource,
- UICollectionViewDelegateFlowLayout,
- UIScrollViewDelegate,
- SDPhotoBrowserDelegate
- >
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) UIScrollView *scrollView;
- @property (nonatomic, strong) NSArray *dataArr;
- @property (nonatomic, strong) NSMutableArray *imgViewArray;
- @end
- static NSString *cellID = @"cellID";
- @implementation DRImageContainerView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self initSubViews];
- }
- return self;
- }
- - (void)initSubViews {
- // UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
- // flowLayout.minimumLineSpacing = 5;
- // flowLayout.minimumInteritemSpacing = 5;
- // flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- // self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, 150) collectionViewLayout:flowLayout];
- // self.collectionView.delegate = self;
- // self.collectionView.dataSource = self;
- // self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xFDF8F1];
- // self.collectionView.showsVerticalScrollIndicator = NO;
- // self.collectionView.showsHorizontalScrollIndicator = NO;
- // [self.collectionView registerClass:[DRContainerImageCell class] forCellWithReuseIdentifier:cellID];
- // [self addSubview:self.collectionView];
-
- self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
- [self addSubview:self.scrollView];
- }
- - (void)reloadWithDataArray:(NSArray *)dataArr {
- self.dataArr = dataArr;
- CGFloat width = 147;
- [self.scrollView removeAllSubviews];
- [self.imgViewArray removeAllObjects];
- UIImageView *lastImgView;
- for (int i = 0; i<self.dataArr.count; i++) {
- DRShareImageCellModel *model = self.dataArr[i];
- UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(15+(width+5)*i, 2, width, width)];
- imgView.contentMode = UIViewContentModeScaleAspectFit;
- imgView.backgroundColor = [UIColor whiteColor];
- imgView.userInteractionEnabled = YES;
- imgView.tag = 10000+i;
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgAction:)];
- [imgView addGestureRecognizer:tap];
- [self.imgViewArray addObject:imgView];
-
- UIButton *selBtn = [[UIButton alloc] initWithFrame:CGRectMake(width-40, 0, 40, 40)];
- [selBtn setImage:[UIImage imageNamed:@"cell_sel_n"] forState:UIControlStateNormal];
- [selBtn setImage:[UIImage imageNamed:@"cell_sel_s"] forState:UIControlStateSelected];
- selBtn.selected = model.isSelected;
- [selBtn addTarget:self action:@selector(selectedAction:) forControlEvents:UIControlEventTouchUpInside];
- selBtn.tag = 1000+i;
- [imgView addSubview:selBtn];
-
- [self.scrollView addSubview:imgView];
- if (i == 0) {
- if (!model.shareImg) {
- UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
- indicatorView.frame= CGRectMake(0, 0, 50, 50);
- indicatorView.color = [UIColor homeRedColor];
- indicatorView.center = CGPointMake(imgView.width/2, imgView.height/2);
- [indicatorView startAnimating];
- [imgView addSubview:indicatorView];
- }
-
- imgView.image = model.shareImg;
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, width-28, width, 28)];
- label.backgroundColor = [UIColor YHColorWithHex:0xFF891A];
- label.textAlignment = NSTextAlignmentCenter;
- label.textColor = [UIColor whiteColor];
- label.font = [UIFont systemFontOfSize:13];
- label.text = @"二维码推广图";
- [imgView addSubview:label];
- }else {
- [imgView sd_setFadeImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:nil options:0 progress:nil completed:nil];
- }
- lastImgView = imgView;
- }
- // [self.collectionView reloadData];
- self.scrollView.contentSize = CGSizeMake(lastImgView.right+15, 150);
- }
- - (void)selectedAction:(UIButton *)sender {
- NSInteger index = sender.tag - 1000;
- for (int i = 0; i<self.dataArr.count; i++) {
- DRShareImageCellModel *model = self.dataArr[i];
- if (i == index) {
- model.isSelected = YES;
- }else {
- model.isSelected = NO;
- }
- }
-
- [self reloadWithDataArray:self.dataArr];
-
- }
- - (void)tapImgAction:(UITapGestureRecognizer *)tap {
- UIImageView *imgView = (UIImageView *)tap.view;
- NSInteger index = imgView.tag - 10000;
- SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
- browser.sourceImagesContainerView = self.scrollView;
- browser.currentImageIndex = index;
- browser.delegate = self;
- browser.imageCount = self.dataArr.count;
- [browser show];
- }
- //#pragma mark ======
- //- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- // return self.dataArr.count;
- //}
- //
- //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- // return CGSizeMake(147, 147);
- //}
- //
- //
- //
- //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
- // return UIEdgeInsetsMake(0, 15, 0, 5);
- //}
- //
- //- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- //
- // DRContainerImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
- // DRShareImageCellModel *model = self.dataArr[indexPath.row];
- // cell.selectBlock = ^{
- // for (int i = 0; i < self.dataArr.count; i++) {
- // DRShareImageCellModel *item = self.dataArr[i];
- // if (i == indexPath.row) {
- // item.isSelected = YES;
- // }else {
- // item.isSelected = NO;
- // }
- // }
- // [self.collectionView reloadData];
- // };
- // cell.model = model;
- // if (indexPath.row == 0) {
- // cell.qrImgLabel.hidden = NO;
- // }else {
- // cell.qrImgLabel.hidden = YES;
- // }
- // return cell;
- //}
- //
- //- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- // SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
- // browser.sourceImagesContainerView = self.collectionView;
- // browser.currentImageIndex = indexPath.row;
- // browser.delegate = self;
- // browser.imageCount = self.dataArr.count;
- // [browser show];
- //
- //}
- #pragma mark ----SDPhotoBrowser
- - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
- UIImageView *imgView = self.imgViewArray[index];
- if (imgView.image) {
- return imgView.image;
- }
- return [UIImage imageNamed:@"placeHolder"];
- }
- - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
- DRShareImageCellModel *model = self.dataArr[index];
- NSString *imgUrl = model.imgUrl;
- return [NSURL URLWithString:imgUrl];
- }
- #pragma mark ----
- - (NSMutableArray *)imgViewArray {
- if (!_imgViewArray) {
- _imgViewArray = [NSMutableArray array];
- }
- return _imgViewArray;
- }
- @end
|