// // ASHomeNewInCellTableViewCell.m // Asteria // // Created by iOS on 2023/6/9. // #import "ASHomeNewInCellTableViewCell.h" #import "KWScrollOffsetView.h" #import "ASHomeNewInSubCollectCell.h" @interface ASHomeNewInCellTableViewCell () @property (nonatomic, strong) UIView *colorBgV; @property (nonatomic, strong) UICollectionView *collectV; @property (nonatomic, strong) UILabel *titleLb; @property (nonatomic, strong) KWScrollOffsetView *offsetV; @property (nonatomic, strong) UIView *bottomV; @property (nonatomic, strong) UIButton *moreBt; @property (nonatomic, strong) ASHomeMainListModel *model; @end @implementation ASHomeNewInCellTableViewCell -(void)setData:(ASHomeMainListModel *)model { self.model = model; self.titleLb.text = self.model.title; self.offsetV.hidden = model.productList.count <= 1; [self.collectV reloadData]; } - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self loadSubV]; } return self; } - (void)loadSubV { self.selectionStyle = UITableViewCellSelectionStyleNone; self.contentView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"]; self.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"]; self.offsetV.backgroundColor = _B1B1B1; self.offsetV.offsetV.backgroundColor = Col_FFF; [self.contentView addSubview:self.colorBgV]; [self.colorBgV mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.contentView); }]; [self.contentView addSubview:self.titleLb]; [self.contentView addSubview:self.collectV]; [self.contentView addSubview:self.offsetV]; [self.contentView addSubview:self.bottomV]; [self.bottomV addSubview:self.moreBt]; [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView).offset(50); make.centerX.equalTo(self.contentView); make.left.greaterThanOrEqualTo(self.contentView).offset(20); make.height.equalTo(@30); make.width.greaterThanOrEqualTo(@80); }]; CGFloat collH = (KScreenWidth-20) + productHWithOutImg+10; [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(0); make.right.equalTo(self.contentView).offset(0); make.height.equalTo([NSNumber numberWithFloat:collH]); make.top.equalTo(self.titleLb.mas_bottom).offset(30); }]; [self.offsetV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.contentView); make.height.equalTo(@2); make.width.equalTo(@137); make.top.equalTo(self.collectV.mas_bottom).offset(11); }]; [self.bottomV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.contentView); make.height.equalTo(@(20+36)); make.leading.equalTo(self.contentView); make.top.equalTo(self.offsetV.mas_bottom).offset(20); make.bottom.equalTo(self.contentView).offset(-40); }]; [self.moreBt mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(@200); make.height.equalTo(@36); make.centerX.equalTo(self.bottomV); make.centerY.equalTo(self.bottomV); }]; dispatch_async(dispatch_get_main_queue(), ^{ [UIView viewAddHorColorBg:self.colorBgV colorArr:@[ (id)(_043632.CGColor), (id)(_0E0E0F.CGColor), ] startP:CGPointMake(0.5, 0.1) endP:CGPointMake(0.5, 1)]; }); } // MARK: - UICollectionViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (self.offsetV.isHidden) { return; } CGFloat offset = scrollView.contentOffset.x; CGFloat itemWidth = (KScreenWidth-20); NSArray *arr = self.model.productList; if (arr.count > 0) { CGFloat maxOffset = (arr.count * (itemWidth + 10) - 10 ) - (KScreenWidth-20); self.offsetV.offset = offset/maxOffset; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.productClick) { NSArray *arr = self.model.productList; ASProductBaseModel *m = arr[indexPath.row]; self.productClick(indexPath.row, m); } } // MARK: - UICollectionViewDataSource - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { ASHomeNewInSubCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASHomeNewInSubCollectCell" forIndexPath:indexPath]; NSArray *arr = self.model.productList; ASProductBaseModel *m = arr[indexPath.row]; cell.model = m; __weak typeof(self) weakSelf = self; cell.contView.addCartBlock = ^(ASProductBaseModel * _Nonnull model) { if (weakSelf.addCartBlock) { weakSelf.addCartBlock(indexPath.row, model); } }; return cell; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { NSArray *arr = self.model.productList; return arr.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat marg = 10; CGFloat w = (KScreenWidth-2*marg); CGFloat collH = (KScreenWidth-20) + productHWithOutImg+10; return CGSizeMake(w, collH); } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10; } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 10; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 10, 0, 10); } // MARK: - subvs - (UILabel *)titleLb { if (!_titleLb) { UILabel *lb = [[UILabel alloc] init]; lb.textColor = [UIColor whiteColor]; lb.font = [UIFont fontWithName:Rob_Bold size:24]; lb.textAlignment = NSTextAlignmentCenter; _titleLb = lb; } return _titleLb; } - (KWScrollOffsetView *)offsetV { if (!_offsetV) { KWScrollOffsetView *v = [[KWScrollOffsetView alloc] initWithFrame:CGRectMake(0, 0, 137, 2)]; v.clipsToBounds = true; v.offWidth = 54; v.offset = 0; _offsetV = v; } return _offsetV; } -(UICollectionView *)collectV { if (!_collectV) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; CGFloat collH = (KScreenWidth-30)/2 + 126; layout.itemSize = CGSizeMake((KScreenWidth - 30)/2, collH); UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout]; collV.backgroundColor = [UIColor clearColor]; collV.alwaysBounceHorizontal = true; // collV.pagingEnabled = true; collV.delegate = self; collV.dataSource = self; collV.showsHorizontalScrollIndicator = false; [collV registerClass:[ASHomeNewInSubCollectCell class] forCellWithReuseIdentifier:@"ASHomeNewInSubCollectCell"]; _collectV = collV; } return _collectV; } -(UIView *)bottomV { if (!_bottomV) { UIView *v = [UIView baseV]; v.backgroundColor = UIColor.clearColor; v.clipsToBounds = true; _bottomV = v; } return _bottomV; } - (UIButton *)moreBt { if (!_moreBt) { UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom]; [bt setTitle:@"VIEW MORE" forState:UIControlStateNormal]; [bt setTitleColor:Col_FFF forState:UIControlStateNormal]; bt.titleLabel.font = [UIFont fontWithName:Rob_Regular size:16]; bt.layer.cornerRadius = 4; bt.layer.borderColor = Col_FFF.CGColor; bt.layer.borderWidth = 1; bt.layer.masksToBounds = true; _moreBt = bt; } return _moreBt; } - (UIView *)colorBgV { if (!_colorBgV) { UIView *v = [UIView baseV]; v.backgroundColor = _043632; _colorBgV = v; } return _colorBgV; } @end