123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- //
- // DRRecomPhotoView.m
- // YouHuiProject
- //
- // Created by 小花 on 2019/1/7.
- // Copyright © 2019年 kuxuan. All rights reserved.
- //
- #import "DRRecomPhotoView.h"
- #import "SDPhotoBrowser.h"
- #import "UIView+SDAutoLayout.h"
- @interface DRRecomPhotoView ()<SDPhotoBrowserDelegate>
- @property (nonatomic, strong) NSArray *imageViewsArray;
- @end
- @implementation DRRecomPhotoView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- if (self = [super initWithFrame:frame]) {
- [self setup];
- }
- return self;
- }
- - (void)setup
- {
- NSMutableArray *temp = [NSMutableArray new];
-
-
- for (int i = 0; i < 9; i++) {
- UIImageView *imageView = [UIImageView new];
- [self addSubview:imageView];
- imageView.userInteractionEnabled = YES;
- imageView.contentMode = UIViewContentModeScaleAspectFill;
- imageView.layer.masksToBounds = YES;
- imageView.tag = i;
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
- [imageView addGestureRecognizer:tap];
-
- [temp addObject:imageView];
-
- }
-
- self.imageViewsArray = [temp copy];
- }
- - (void)setPicPathStringsArray:(NSArray *)picPathStringsArray
- {
- _picPathStringsArray = picPathStringsArray;
-
- for (long i = _picPathStringsArray.count; i < self.imageViewsArray.count; i++) {
- UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];
- imageView.hidden = YES;
- }
-
- if (_picPathStringsArray.count == 0) {
- self.height = 0;
- self.fixedHeight = @(0);
- return;
- }
-
- CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray];
- CGFloat itemH = 0;
- // if (_picPathStringsArray.count == 1) {
- // UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject];
- // if (image.size.width) {
- // itemH = image.size.height / image.size.width * itemW;
- // }
- // } else {
- // itemH = itemW;
- // }
- itemH = itemW;
- long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray];
- CGFloat margin = 5;
-
- [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- long columnIndex = idx % perRowItemCount;
- long rowIndex = idx / perRowItemCount;
- UIImageView *imageView = [_imageViewsArray objectAtIndex:idx];
- imageView.hidden = NO;
- [imageView yy_setImageWithURL:[NSURL URLWithString:obj] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
- imageView.backgroundColor = [UIColor clearColor];
- imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
-
- }];
-
- CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
- int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
- CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
- self.width = w;
- self.height = h;
-
- self.fixedHeight = @(h);
- self.fixedWidth = @(w);
- }
- #pragma mark - private actions
- - (void)tapImageView:(UITapGestureRecognizer *)tap
- {
- UIView *imageView = tap.view;
- SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
- browser.currentImageIndex = imageView.tag;
- browser.sourceImagesContainerView = self;
- browser.imageCount = self.picPathStringsArray.count;
- browser.delegate = self;
- [browser show];
-
- [MobClick event:OptimizingCircleMorningClick];
- }
- - (CGFloat)itemWidthForPicPathArray:(NSArray *)array
- {
- if (array.count == 1) {
- return 144;
- } else {
- CGFloat margin = 5;
- CGFloat w = ([UIScreen mainScreen].bounds.size.width-20-margin*2)/3;
- return w;
- }
- }
- - (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
- {
- // if (array.count < 3) {
- // return array.count;
- // } else if (array.count <= 4) {
- // return 2;
- // } else {
- // return 3;
- // }
- if (array.count<=3) {
- return array.count;
- }
- return 3;
- }
- #pragma mark - SDPhotoBrowserDelegate
- - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
- {
- NSString *imageName = self.picPathStringsArray[index];
- NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
- return url;
- }
- - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
- {
- UIImageView *imageView = self.subviews[index];
- return imageView.image;
- }
- @end
|