// // YOUPAIRightCollectionView.m // MSYOUPAI // // Created by xiaohaoran on 2022/3/1. // Copyright © 2022 MS. All rights reserved. // #import "YOUPAIRightCollectionView.h" #import "YOUPAIRightCollectionContentCell.h" #import "UIImage+Extension.h" @interface YOUPAIRightCollectionView () @property (nonatomic, strong) UICollectionView *youpaipcollectionView; @property (assign, nonatomic) NSIndexPath *selIndex; //单选选中的行 @property(nonatomic,assign)BOOL isBigImage;//大图是否滚动 @end @implementation YOUPAIRightCollectionView -(instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self youpaifsetSubView]; } return self; } -(void)youpaifsetSubView{ UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.youpaipcollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout]; [self addSubview:self.youpaipcollectionView]; [self.youpaipcollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(0); }]; // self.youpaipcollectionView.userInteractionEnabled = NO; self.youpaipcollectionView.backgroundColor = [UIColor clearColor]; self.youpaipcollectionView.showsVerticalScrollIndicator = NO; self.youpaipcollectionView.showsHorizontalScrollIndicator = NO; // self.youpaipcollectionView.bounces = NO; [self.youpaipcollectionView registerClass:[YOUPAIRightCollectionContentCell class] forCellWithReuseIdentifier:NSStringFromClass([YOUPAIRightCollectionContentCell class])]; self.youpaipcollectionView.delegate = self; self.youpaipcollectionView.dataSource = self; } #pragma mark /** 返回区数 @param collectionView 集合视图 @return 返回区数 */ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } /** 返回项目数 @param collectionView 集合视图 @param section 区 @return 项目数 */ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.Model.youpaipalbums.count; } -(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ // NSString *imageUrl = self.albumArray[indexPath.item]; YOUPAIRightCollectionContentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YOUPAIRightCollectionContentCell class]) forIndexPath:indexPath]; YOUPAIAlbumModel *model = self.Model.youpaipalbums[indexPath.item]; NSString *utlStr = [NSString stringWithFormat:@"%@/%@",[LCSaveData getImageUrl]?[LCSaveData getImageUrl]:BaseImgUrl,model.youpaipurl] ; [cell.youpaipiconImageView sd_setImageWithURL:[NSURL URLWithString:utlStr] placeholderImage:nil]; if (indexPath.item ==0) { if (self.isBigImage) { cell.cycleShow = NO; }else{ cell.cycleShow = YES; self.selIndex = [NSIndexPath indexPathForItem:0 inSection:0]; } if (model.youpaipis_video ==1) { cell.youpaipplayImageView.hidden = NO; cell.youpaipiconImageView.image = [UIImage imageWithVideo:[NSURL URLWithString:utlStr]]; }else{ cell.youpaipplayImageView.hidden = YES; } }else{ cell.youpaipplayImageView.hidden = YES; } return cell; } //+ (UIImage *)imageWithVideo:(NSURL *)videoURL //大图滚动后小图显示位置 -(void)setScrollToItemAtIndex:(NSInteger)index{ self.isBigImage = YES; [self.youpaipcollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES]; YOUPAIRightCollectionContentCell *cell = (YOUPAIRightCollectionContentCell*)[self.youpaipcollectionView cellForItemAtIndexPath:self.selIndex]; cell.cycleShow = NO; //记录当前的选择的位置 _selIndex = [NSIndexPath indexPathForItem:index inSection:0]; YOUPAIRightCollectionContentCell *cell1 = (YOUPAIRightCollectionContentCell*)[self.youpaipcollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]; cell1.cycleShow = YES; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ YOUPAIRightCollectionContentCell *cell = (YOUPAIRightCollectionContentCell*)[self.youpaipcollectionView cellForItemAtIndexPath:self.selIndex]; cell.cycleShow = NO; //记录当前的选择的位置 _selIndex = indexPath; YOUPAIRightCollectionContentCell *cell1 = (YOUPAIRightCollectionContentCell*)[self.youpaipcollectionView cellForItemAtIndexPath:indexPath]; cell1.cycleShow = YES; if (self.didSelectImageIndex) { self.didSelectImageIndex(indexPath.item); } } #pragma mark UICollectionViewDelegateFlowLayout /** 项目大小 @param collectionView 集合视图 @param collectionViewLayout 布局 @param indexPath 布局 @return 大小 */ - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(44, 44); } #pragma mark - X间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 5.0; } //设置段落的内边距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0,0,0,0);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) } @end