Files
yinmeng-ios/xplan-ios/Main/Monents/View/SubViews/XPMonentsPhotoView.m
2022-11-11 17:46:37 +08:00

301 lines
11 KiB
Objective-C

//
// XPMonentsPhotoView.m
// xplan-ios
//
// Created by 冯硕 on 2022/5/12.
//
#import "XPMonentsPhotoView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "ThemeColor.h"
#import "NetImageView.h"
#import "XPMonentsLayoutConfig.h"
#import "NSArray+Safe.h"
///Model
#import "MonentsInfoModel.h"
@interface XPMonentsPhotoView ()
///第一个
@property (nonatomic,strong) NetImageView *firstImageView;
///第二个
@property (nonatomic,strong) NetImageView *secondImageView;
///第三个
@property (nonatomic,strong) NetImageView *thirdImageView;
///第四个
@property (nonatomic,strong) NetImageView *fourthImageView;
///第五个
@property (nonatomic,strong) NetImageView *fifthImageView;
///第六个
@property (nonatomic,strong) NetImageView *sixthImageView;
///第七个
@property (nonatomic,strong) NetImageView *sevenImageView;
///第八个
@property (nonatomic,strong) NetImageView *eighthImageView;
///第九个
@property (nonatomic,strong) NetImageView *ninthImageView;
@property (nonatomic,strong) NSArray<NetImageView *> *subViewArray;
@end
@implementation XPMonentsPhotoView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.firstImageView];
[self addSubview:self.secondImageView];
[self addSubview:self.thirdImageView];
[self addSubview:self.fourthImageView];
[self addSubview:self.fifthImageView];
[self addSubview:self.sixthImageView];
[self addSubview:self.sevenImageView];
[self addSubview:self.eighthImageView];
[self addSubview:self.ninthImageView];
self.subViewArray = @[self.firstImageView, self.secondImageView, self.thirdImageView, self.fourthImageView, self.fifthImageView, self.sixthImageView, self.sevenImageView, self.eighthImageView, self.ninthImageView];
}
- (void)hiddenAllImageView {
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.hidden = YES;
}];
}
#pragma mark - Event Response
- (void)tapImageView:(UITapGestureRecognizer *)tap {
NetImageView * imageView = (NetImageView *)tap.view;
if (imageView.imageUrl.length > 0 && self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsPhotoView:didClickImage:)]) {
[self.delegate xPMonentsPhotoView:self didClickImage:imageView.tag - 1001];
}
}
#pragma mark - Getters And Setters
- (void)setDynamicResList:(NSArray<MonentsPicInfoModel *> *)dynamicResList {
_dynamicResList = dynamicResList;
[self hiddenAllImageView];
if (_dynamicResList.count == 0) {
} else if (_dynamicResList.count == 1) {
self.firstImageView.hidden = NO;
[self.firstImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(kMONENTS_PIC_ONE_WIDTH, kMONENTS_PIC_ONE_WIDTH));
make.left.top.mas_equalTo(self);
}];
MonentsPicInfoModel * picInfo = [_dynamicResList safeObjectAtIndex1:0];
self.firstImageView.imageUrl = picInfo.resUrl;
} else if(_dynamicResList.count == 2) {
self.firstImageView.hidden = NO;
CGFloat itemWidth = (kMONENTS_CONTENT_MAX_WIDTH - kMONENTS_PIC_SPACE) / 2;
[self.firstImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
make.left.top.mas_equalTo(self);
}];
MonentsPicInfoModel * picInfo = [_dynamicResList safeObjectAtIndex1:0];
self.firstImageView.imageUrl = picInfo.resUrl;
self.secondImageView.hidden = NO;
[self.secondImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
make.top.mas_equalTo(self);
make.left.mas_equalTo(self.firstImageView.mas_right).offset(kMONENTS_PIC_SPACE);
}];
MonentsPicInfoModel * secondPicInfo = [_dynamicResList safeObjectAtIndex1:1];
self.secondImageView.imageUrl = secondPicInfo.resUrl;
} else{
for (int i = 0; i < _dynamicResList.count; i++) {
MonentsPicInfoModel * picInfo = [_dynamicResList safeObjectAtIndex1:i];
if (i < self.subViewArray.count) {
NetImageView * imageView = [self.subViewArray safeObjectAtIndex1:i];
imageView.hidden = NO;
imageView.imageUrl = picInfo.resUrl;
}
}
CGFloat itemWidth = (kMONENTS_CONTENT_MAX_WIDTH - kMONENTS_PIC_SPACE * 2) / 3;
for (int i = 0; i < self.subViewArray.count; i++) {
NSInteger page = i % 3;
NSInteger line = i / 3;
NetImageView * imageView = [self.subViewArray safeObjectAtIndex1:i];
[imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
make.left.mas_equalTo(page * (itemWidth + kMONENTS_PIC_SPACE));
make.top.mas_equalTo(line * (itemWidth + kMONENTS_PIC_SPACE));
}];
}
}
}
- (NetImageView *)firstImageView {
if (!_firstImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_firstImageView = [[NetImageView alloc] initWithConfig:config];
_firstImageView.layer.masksToBounds = YES;
_firstImageView.layer.cornerRadius = 12;
_firstImageView.contentMode = UIViewContentModeScaleAspectFill;
_firstImageView.tag = 1000 + 1;
_firstImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[_firstImageView addGestureRecognizer:tap];
}
return _firstImageView;
}
- (NetImageView *)secondImageView {
if (!_secondImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_secondImageView = [[NetImageView alloc] initWithConfig:config];
_secondImageView.layer.masksToBounds = YES;
_secondImageView.layer.cornerRadius = 12;
_secondImageView.contentMode = UIViewContentModeScaleAspectFill;
_secondImageView.tag = 1000 + 2;
_secondImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[_secondImageView addGestureRecognizer:tap];
}
return _secondImageView;
}
- (NetImageView *)thirdImageView {
if (!_thirdImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_thirdImageView = [[NetImageView alloc] initWithConfig:config];
_thirdImageView.layer.masksToBounds = YES;
_thirdImageView.layer.cornerRadius = 12;
_thirdImageView.contentMode = UIViewContentModeScaleAspectFill;
_thirdImageView.tag = 1000 + 3;
_thirdImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[_thirdImageView addGestureRecognizer:tap];
}
return _thirdImageView;
}
- (NetImageView *)fourthImageView {
if (!_fourthImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_fourthImageView = [[NetImageView alloc] initWithConfig:config];
_fourthImageView.layer.masksToBounds = YES;
_fourthImageView.layer.cornerRadius = 12;
_fourthImageView.contentMode = UIViewContentModeScaleAspectFill;
_fourthImageView.tag = 1000 + 4;
_fourthImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[_fourthImageView addGestureRecognizer:tap];
}
return _fourthImageView;
}
- (NetImageView *)fifthImageView {
if (!_fifthImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_fifthImageView = [[NetImageView alloc] initWithConfig:config];
_fifthImageView.layer.masksToBounds = YES;
_fifthImageView.layer.cornerRadius = 12;
_fifthImageView.contentMode = UIViewContentModeScaleAspectFill;
_fifthImageView.tag = 1000 + 5;
_fifthImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[_fifthImageView addGestureRecognizer:tap];
}
return _fifthImageView;
}
- (NetImageView *)sixthImageView {
if (!_sixthImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_sixthImageView = [[NetImageView alloc] initWithConfig:config];
_sixthImageView.layer.masksToBounds = YES;
_sixthImageView.layer.cornerRadius = 12;
_sixthImageView.contentMode = UIViewContentModeScaleAspectFill;
_sixthImageView.tag = 1000 + 6;
_sixthImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[_sixthImageView addGestureRecognizer:tap];
}
return _sixthImageView;
}
- (NetImageView *)sevenImageView {
if (!_sevenImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_sevenImageView = [[NetImageView alloc] initWithConfig:config];
_sevenImageView.layer.masksToBounds = YES;
_sevenImageView.layer.cornerRadius = 12;
_sevenImageView.contentMode = UIViewContentModeScaleAspectFill;
_sevenImageView.tag = 1000 + 7;
_sevenImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[_sevenImageView addGestureRecognizer:tap];
}
return _sevenImageView;
}
- (NetImageView *)eighthImageView {
if (!_eighthImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_eighthImageView = [[NetImageView alloc] initWithConfig:config];
_eighthImageView.layer.masksToBounds = YES;
_eighthImageView.layer.cornerRadius = 12;
_eighthImageView.contentMode = UIViewContentModeScaleAspectFill;
_eighthImageView.tag = 1000 + 8;
_eighthImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[_eighthImageView addGestureRecognizer:tap];
}
return _eighthImageView;
}
- (NetImageView *)ninthImageView {
if (!_ninthImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_ninthImageView = [[NetImageView alloc] initWithConfig:config];
_ninthImageView.layer.masksToBounds = YES;
_ninthImageView.layer.cornerRadius = 12;
_ninthImageView.contentMode = UIViewContentModeScaleAspectFill;
_ninthImageView.tag = 1000 + 9;
_ninthImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[_ninthImageView addGestureRecognizer:tap];
}
return _ninthImageView;
}
@end