YOUPAIRightCollectionView.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // YOUPAIRightCollectionView.m
  3. // MSYOUPAI
  4. //
  5. // Created by xiaohaoran on 2022/3/1.
  6. // Copyright © 2022 MS. All rights reserved.
  7. //
  8. #import "YOUPAIRightCollectionView.h"
  9. #import "YOUPAIRightCollectionContentCell.h"
  10. #import "UIImage+Extension.h"
  11. @interface YOUPAIRightCollectionView ()<UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
  12. @property (nonatomic, strong) UICollectionView *youpaipcollectionView;
  13. @property (assign, nonatomic) NSIndexPath *selIndex; //单选选中的行
  14. @property(nonatomic,assign)BOOL isBigImage;//大图是否滚动
  15. @end
  16. @implementation YOUPAIRightCollectionView
  17. -(instancetype)initWithFrame:(CGRect)frame{
  18. if (self = [super initWithFrame:frame]) {
  19. [self youpaifsetSubView];
  20. }
  21. return self;
  22. }
  23. -(void)youpaifsetSubView{
  24. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  25. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  26. self.youpaipcollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  27. [self addSubview:self.youpaipcollectionView];
  28. [self.youpaipcollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.edges.mas_equalTo(0);
  30. }];
  31. // self.youpaipcollectionView.userInteractionEnabled = NO;
  32. self.youpaipcollectionView.backgroundColor = [UIColor clearColor];
  33. self.youpaipcollectionView.showsVerticalScrollIndicator = NO;
  34. self.youpaipcollectionView.showsHorizontalScrollIndicator = NO;
  35. // self.youpaipcollectionView.bounces = NO;
  36. [self.youpaipcollectionView registerClass:[YOUPAIRightCollectionContentCell class] forCellWithReuseIdentifier:NSStringFromClass([YOUPAIRightCollectionContentCell class])];
  37. self.youpaipcollectionView.delegate = self;
  38. self.youpaipcollectionView.dataSource = self;
  39. }
  40. #pragma mark <UICollectionViewDataSource>
  41. /**
  42. 返回区数
  43. @param collectionView 集合视图
  44. @return 返回区数
  45. */
  46. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  47. return 1;
  48. }
  49. /**
  50. 返回项目数
  51. @param collectionView 集合视图
  52. @param section 区
  53. @return 项目数
  54. */
  55. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  56. return self.Model.youpaipalbums.count;
  57. }
  58. -(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  59. // NSString *imageUrl = self.albumArray[indexPath.item];
  60. YOUPAIRightCollectionContentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YOUPAIRightCollectionContentCell class]) forIndexPath:indexPath];
  61. YOUPAIAlbumModel *model = self.Model.youpaipalbums[indexPath.item];
  62. NSString *utlStr = [NSString stringWithFormat:@"%@/%@",[LCSaveData getImageUrl]?[LCSaveData getImageUrl]:BaseImgUrl,model.youpaipurl] ;
  63. [cell.youpaipiconImageView sd_setImageWithURL:[NSURL URLWithString:utlStr] placeholderImage:nil];
  64. if (indexPath.item ==0) {
  65. if (self.isBigImage) {
  66. cell.cycleShow = NO;
  67. }else{
  68. cell.cycleShow = YES;
  69. self.selIndex = [NSIndexPath indexPathForItem:0 inSection:0];
  70. }
  71. if (model.youpaipis_video ==1) {
  72. cell.youpaipplayImageView.hidden = NO;
  73. cell.youpaipiconImageView.image = [UIImage imageWithVideo:[NSURL URLWithString:utlStr]];
  74. }else{
  75. cell.youpaipplayImageView.hidden = YES;
  76. }
  77. }else{
  78. cell.youpaipplayImageView.hidden = YES;
  79. }
  80. return cell;
  81. }
  82. //+ (UIImage *)imageWithVideo:(NSURL *)videoURL
  83. //大图滚动后小图显示位置
  84. -(void)setScrollToItemAtIndex:(NSInteger)index{
  85. self.isBigImage = YES;
  86. [self.youpaipcollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
  87. YOUPAIRightCollectionContentCell *cell = (YOUPAIRightCollectionContentCell*)[self.youpaipcollectionView cellForItemAtIndexPath:self.selIndex];
  88. cell.cycleShow = NO;
  89. //记录当前的选择的位置
  90. _selIndex = [NSIndexPath indexPathForItem:index inSection:0];
  91. YOUPAIRightCollectionContentCell *cell1 = (YOUPAIRightCollectionContentCell*)[self.youpaipcollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
  92. cell1.cycleShow = YES;
  93. }
  94. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  95. YOUPAIRightCollectionContentCell *cell = (YOUPAIRightCollectionContentCell*)[self.youpaipcollectionView cellForItemAtIndexPath:self.selIndex];
  96. cell.cycleShow = NO;
  97. //记录当前的选择的位置
  98. _selIndex = indexPath;
  99. YOUPAIRightCollectionContentCell *cell1 = (YOUPAIRightCollectionContentCell*)[self.youpaipcollectionView cellForItemAtIndexPath:indexPath];
  100. cell1.cycleShow = YES;
  101. if (self.didSelectImageIndex) {
  102. self.didSelectImageIndex(indexPath.item);
  103. }
  104. }
  105. #pragma mark UICollectionViewDelegateFlowLayout
  106. /**
  107. 项目大小
  108. @param collectionView 集合视图
  109. @param collectionViewLayout 布局
  110. @param indexPath 布局
  111. @return 大小
  112. */
  113. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  114. return CGSizeMake(44, 44);
  115. }
  116. #pragma mark - X间距
  117. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  118. return 5.0;
  119. }
  120. //设置段落的内边距
  121. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  122. {
  123. return UIEdgeInsetsMake(0,0,0,0);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
  124. }
  125. @end