ASHomeLookingCell.m 6.4 KB

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