ASHomeLookingCell.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // ASHomeLookingCell.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/6/10.
  6. //
  7. #import "ASHomeLookingCell.h"
  8. #import "KWScrollOffsetView.h"
  9. #import "ASHomeLookingCollCell.h"
  10. @interface ASHomeLookingCell ()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
  11. @property (nonatomic, strong) UICollectionView *collectV;
  12. @property (nonatomic, strong) UILabel *titleLb;
  13. @property (nonatomic, strong) KWScrollOffsetView *offsetV;
  14. @end
  15. @implementation ASHomeLookingCell
  16. - (void)setModel:(ASHomeMainListModel *)model {
  17. _model = model;
  18. self.titleLb.text = model.title;
  19. self.offsetV.hidden = model.titleArr.count <= 1;
  20. [self.collectV reloadData];
  21. }
  22. - (void)awakeFromNib {
  23. [super awakeFromNib];
  24. // Initialization code
  25. }
  26. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  27. [super setSelected:selected animated:animated];
  28. // Configure the view for the selected state
  29. }
  30. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  31. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  32. if (self) {
  33. [self loadSubV];
  34. }
  35. return self;
  36. }
  37. - (void)loadSubV {
  38. self.selectionStyle = UITableViewCellSelectionStyleNone;
  39. self.contentView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
  40. self.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
  41. [self.contentView addSubview:self.titleLb];
  42. [self.contentView addSubview:self.collectV];
  43. [self.contentView addSubview:self.offsetV];
  44. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.top.equalTo(self.contentView).offset(50);
  46. make.centerX.equalTo(self.contentView);
  47. make.left.greaterThanOrEqualTo(self.contentView).offset(20);
  48. make.height.equalTo(@30);
  49. }];
  50. CGFloat collH = ((KScreenWidth-30)/2 + 28)*2 + 10;
  51. [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.left.equalTo(self.contentView).offset(0);
  53. make.right.equalTo(self.contentView).offset(-0);
  54. make.height.equalTo([NSNumber numberWithFloat:collH]);
  55. make.top.equalTo(self.titleLb.mas_bottom).offset(30);
  56. }];
  57. [self.offsetV mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.centerX.equalTo(self.contentView);
  59. make.height.equalTo(@2);
  60. make.width.equalTo(@137);
  61. make.top.equalTo(self.collectV.mas_bottom).offset(40);
  62. make.bottom.equalTo(self.contentView.mas_bottom).offset(-50);
  63. }];
  64. }
  65. // MARK: - delegate datasource
  66. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  67. if (self.offsetV.isHidden) {
  68. return;
  69. }
  70. CGFloat offset = scrollView.contentOffset.x;
  71. CGFloat itemWidth = (KScreenWidth-20);
  72. NSArray<ASHomeCategoryModel*> *arr = self.model.titleArr;
  73. if (arr.count > 0) {
  74. CGFloat maxOffset = (arr.count * (itemWidth + 10) - 10 ) - (KScreenWidth-20);
  75. self.offsetV.offset = offset/maxOffset;
  76. }
  77. }
  78. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  79. if (self.itemClick) {
  80. ASHomeCategoryModel *m = self.model.titleArr[indexPath.row];
  81. self.itemClick(indexPath.row, m);
  82. }
  83. }
  84. // MARK: - UICollectionViewDataSource
  85. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  86. ASHomeLookingCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASHomeLookingCollCell" forIndexPath:indexPath];
  87. if (indexPath.row >= self.model.titleArr.count) {
  88. return cell;
  89. }
  90. ASHomeCategoryModel *m = self.model.titleArr[indexPath.row];
  91. [cell setData:m];
  92. return cell;
  93. }
  94. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  95. return self.model.titleArr.count;
  96. }
  97. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  98. CGFloat marg = 10;
  99. CGFloat w = (KScreenWidth-3*marg)/2;
  100. CGFloat collH = ((KScreenWidth-30)/2 + 28);
  101. return CGSizeMake(w, collH);
  102. }
  103. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  104. return 10;
  105. }
  106. -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  107. return 10;
  108. }
  109. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  110. return UIEdgeInsetsMake(0, 10, 0, 10);
  111. }
  112. - (UILabel *)titleLb {
  113. if (!_titleLb) {
  114. UILabel *lb = [[UILabel alloc] init];
  115. lb.textColor = [UIColor blackColor];
  116. lb.font = [UIFont fontWithName:Rob_Bold size:24];
  117. lb.textAlignment = NSTextAlignmentCenter;
  118. _titleLb = lb;
  119. }
  120. return _titleLb;
  121. }
  122. -(UICollectionView *)collectV {
  123. if (!_collectV) {
  124. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  125. layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
  126. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  127. CGFloat collH = ((KScreenWidth-30)/2 + 28)*2 + 10;
  128. CGFloat marg = 10;
  129. CGFloat w = (KScreenWidth-3*marg)/2;
  130. CGFloat itemH = ((KScreenWidth-30)/2 + 28);
  131. layout.itemSize = CGSizeMake(w, itemH);
  132. UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout];
  133. collV.backgroundColor = [UIColor whiteColor];
  134. collV.alwaysBounceHorizontal = true;
  135. collV.scrollEnabled = true;
  136. collV.delegate = self;
  137. collV.dataSource = self;
  138. collV.showsHorizontalScrollIndicator = false;
  139. [collV registerClass:[ASHomeLookingCollCell class] forCellWithReuseIdentifier:@"ASHomeLookingCollCell"];
  140. _collectV = collV;
  141. }
  142. return _collectV;
  143. }
  144. - (KWScrollOffsetView *)offsetV {
  145. if (!_offsetV) {
  146. KWScrollOffsetView *v = [[KWScrollOffsetView alloc] initWithFrame:CGRectMake(0, 0, 137, 2)];
  147. v.clipsToBounds = true;
  148. v.offWidth = 54;
  149. v.offset = 0;
  150. _offsetV = v;
  151. }
  152. return _offsetV;
  153. }
  154. @end