| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 | ////  ASGoodsDetailsSizeSelView.m//  westkissMob////  Created by iOS on 2024/5/19.//#import "ASGoodsDetailsSizeSelView.h"@interface ASGoodsDetailsSizeSelView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>@end@implementation ASGoodsDetailsSizeSelView-(void)setTipStr:(NSString *)tipStr {    _tipStr = tipStr;    CGFloat h = 72;    for (OptionsModel *m in self.optionArr) {        if (m.isLongValue) {            if (m.values.count > 0) {                NSInteger numLine = m.values.count/2 + (m.values.count%2>0 ? 1 : 0);                h += 40 * numLine + 10*(numLine-1) + 20 + 28;            }        } else {            if (m.values.count > 0) {                NSInteger numLine = m.values.count/4 + (m.values.count%4>0 ? 1 : 0);                h += 40 * numLine + 10*(numLine-1) + 20+28;            }        }    }//    if (self.tipStr != nil && self.tipStr.length > 0) {//        h += 60;//    }    if (self.reloadHeight) {        self.reloadHeight(h);    }    if (self.reloadPrice){        self.reloadPrice();    }    [self reloadData];}- (void)setOptionArr:(NSArray<OptionsModel *> *)optionArr {    _optionArr = optionArr;    CGFloat h = 72;    for (OptionsModel *m in optionArr) {        if (m.isLongValue) {            if (m.values.count > 0) {                NSInteger numLine = m.values.count/2 + (m.values.count%2>0 ? 1 : 0);                h += 40 * numLine + 10*(numLine-1) + 20 + 28;            }        } else {            if (m.values.count > 0) {                NSInteger numLine = m.values.count/4 + (m.values.count%4>0 ? 1 : 0);                h += 40 * numLine + 10*(numLine-1) + 20+28;            }        }    }//    if (self.tipStr != nil && self.tipStr.length > 0) {//        h += 60;//    }    if (self.reloadHeight) {        self.reloadHeight(h);    }    if (self.reloadPrice){        self.reloadPrice();    }    [self reloadData];}- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {    UICollectionViewFlowLayout *ly = [[UICollectionViewFlowLayout alloc] init];    ly.scrollDirection = UICollectionViewScrollDirectionVertical;    self = [super initWithFrame:frame collectionViewLayout:ly];    if (self) {        self.num = 1;        self.showsVerticalScrollIndicator = false;        self.showsHorizontalScrollIndicator = false;        self.scrollEnabled = false;        self.backgroundColor = Col_FFF;        self.delegate = self;        self.dataSource = self;        [self registCell];        [self reloadData];    }    return self;}- (void)registCell {    [self registerClass:[APProductDetailSizeHeadV class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"APProductDetailSizeHeadV"];    [self registerClass:[APProductDetailSizeCell class] forCellWithReuseIdentifier:@"APProductDetailSizeCell"];//    [self registerClass:[APProductDetailSizeCell class] forCellWithReuseIdentifier:@"tipCell"];    [self registerClass:[APProductDetailNumStepCell class] forCellWithReuseIdentifier:@"APProductDetailNumStepCell"];}// MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.section < self.optionArr.count) {        OptionsModel *m = self.optionArr[indexPath.section];        if ((!m.is_require.boolValue) && indexPath.row == m.optionSelectTag) {            m.optionSelectTag = -1;            if (self.reloadPrice) {                self.reloadPrice();            }            [collectionView reloadData];            return;        }        if (m.values.count > indexPath.row) {            m.optionSelectTag = indexPath.row;            if (self.reloadPrice) {                self.reloadPrice();            }            [collectionView reloadData];        }            }}- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {//    return self.optionArr.count + 1 + (self.tipStr == nil || self.tipStr.length == 0 ? 0 : 1);    return self.optionArr.count + 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {        if (section < self.optionArr.count) {        OptionsModel *m = self.optionArr[section];        return m.values.count;            }    return 1;    }- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.section < self.optionArr.count) {        OptionsModel *m = self.optionArr[indexPath.section];                APProductDetailSizeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"APProductDetailSizeCell" forIndexPath:indexPath];        if (indexPath.row < m.values.count) {            OptionsValuesM *val = m.values[indexPath.row];            [cell setData:val];        }        [cell setSelType:m.optionSelectTag == indexPath.row isCat:m.isLongValue];        return cell;            }//    if (self.tipStr != nil && self.tipStr.length > 0 && indexPath.section == self.optionArr.count) {//        APProductDetailSizeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"APProductDetailSizeCell" forIndexPath:indexPath];//        [cell setTip:self.tipStr];//        return cell;//    }    APProductDetailNumStepCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"APProductDetailNumStepCell" forIndexPath:indexPath];    __weak typeof(self) weakSelf = self;    cell.numBlock = ^(NSString * _Nonnull num) {        weakSelf.num = num.integerValue;        if (self.reloadPrice) {            self.reloadPrice();        }    };    return cell;    }- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {        APProductDetailSizeHeadV *v = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"APProductDetailSizeHeadV" forIndexPath:indexPath];        if (indexPath.section < self.optionArr.count) {            OptionsModel *m = self.optionArr[indexPath.section];            v.titleLb.text = m.title;        } else {            v.titleLb.text = @"Category";        }        return v;    }    return [[UICollectionReusableView alloc] init];}- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.section < self.optionArr.count) {        OptionsModel *m = self.optionArr[indexPath.section];        if (m.isLongValue) {            CGFloat w = (SCREEN_WIDTH-30-10)/2;            return CGSizeMake(w, 40);        } else {            CGFloat w = (SCREEN_WIDTH-30-10*3)/4;            return CGSizeMake(w, 40);        }            } else {        CGFloat w = (SCREEN_WIDTH-30);        return CGSizeMake(w, 72);    }    }- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {    if (section >= self.optionArr.count){        return  CGSizeZero;    }    return CGSizeMake(SCREEN_WIDTH-30, 28);}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {    return 10;}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {    return 10;}- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {    return UIEdgeInsetsMake(10, 15, 10, 15);}@end@interface APProductDetailSizeCell ()@property (nonatomic, strong) UIView *bgV;@property (nonatomic, strong) UIView *colorBg;@property (nonatomic, strong) UILabel *titleLb;@property (nonatomic, strong) UIImageView *selTipV;@end@implementation APProductDetailSizeCell- (void)setData:(OptionsValuesM *)m {    self.titleLb.text = m.title;}- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        [self loadSubV];    }    return self;}- (void)setSelType:(BOOL)isSel isCat:(BOOL)isCat {    self.colorBg.hidden = true;    TT_ViewRadius(self.bgV, 4);    self.bgV.layer.borderWidth = 1;    self.bgV.backgroundColor = isSel ? _32CFB0 : Col_FFF;    self.bgV.layer.borderColor = isSel ? _32CFB0.CGColor : [UIColor colorWithHexString:@"#E6E6E6"].CGColor;    self.selTipV.hidden = !isSel;    self.titleLb.font = [UIFont fontWithName:(isSel ? Rob_Bold : Rob_Regular ) size:(isCat ? 12 : 14)];}- (void)setTip:(NSString *)tipStr {    self.selTipV.hidden = true;    self.bgV.layer.borderWidth = 0;    self.bgV.backgroundColor = UIColor.clearColor;    self.colorBg.hidden = false;    self.titleLb.text = tipStr;}- (void)loadSubV {    [self.contentView addSubview:self.colorBg];    [self.contentView addSubview:self.bgV];        [self.colorBg mas_makeConstraints:^(MASConstraintMaker *make) {        make.edges.equalTo(self.contentView);    }];        [self.bgV mas_makeConstraints:^(MASConstraintMaker *make) {        make.edges.equalTo(self.contentView);    }];    [self.bgV addSubview:self.titleLb];    [self.bgV addSubview:self.selTipV];    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {        make.top.leading.equalTo(self.bgV).offset(4);        make.bottom.trailing.equalTo(self.bgV).offset(-4);    }];    [self.selTipV mas_makeConstraints:^(MASConstraintMaker *make) {        make.trailing.bottom.equalTo(self.bgV);        make.width.height.equalTo(@12);    }];        }- (void)layoutSubviews {    [super layoutSubviews];    dispatch_async(dispatch_get_main_queue(), ^{//        [self.colorBg lineColor:@[(__bridge id)_FFD1B9.CGColor,(__bridge id)_FFF5F0.CGColor] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5) locations:@[@(0), @(1.0f)]];    });}- (UIView *)bgV {    if (!_bgV) {        UIView *v = [UIView baseV];        v.layer.borderWidth = 1;        v.backgroundColor = Col_FFF;        v.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;        _bgV = v;    }    return _bgV;}- (UILabel *)titleLb {    if (!_titleLb) {        UILabel *lb = [UILabel baseLb];        lb.textAlignment = NSTextAlignmentCenter;        lb.textColor = Col_000;        lb.numberOfLines = 0;        lb.font = [UIFont fontWithName:Rob_Regular size:14];        _titleLb = lb;    }    return _titleLb;}- (UIImageView *)selTipV {    if (!_selTipV) {        UIImageView *imgv = [UIImageView baseImgV];        imgv.contentMode = UIViewContentModeScaleToFill;        imgv.image = [UIImage imageNamed:@"good_sizeSel"];        _selTipV = imgv;    }    return _selTipV;}- (UIView *)colorBg {    if (!_colorBg) {        UIView *v = [UIView baseV];        v.hidden = true;        _colorBg = v;    }    return _colorBg;}@end@interface APProductDetailNumStepCell ()@property (nonatomic, strong) UILabel *titleLb;@property (nonatomic, strong) UIButton *minusBt;@property (nonatomic, strong) UILabel *numLb;@property (nonatomic, strong) UIButton *addBt;@end@implementation APProductDetailNumStepCell- (void)addBtAction:(UIButton *)bt {//    [bt sleep:0.3];    NSInteger num = self.numLb.text.integerValue;    if (num < 1) {        num = 1;    } else {        num += 1;    }    NSString *numStr = [NSString stringWithFormat:@"%ld", num];    self.numLb.text = numStr;    if (self.numBlock) {        self.numBlock(numStr);    }    }- (void)minusBtAction:(UIButton *)bt {//    [bt sleep:0.3];    NSInteger num = self.numLb.text.integerValue;    if (num < 2) {        num = 1;    } else {        num -= 1;    }    NSString *numStr = [NSString stringWithFormat:@"%ld", num];    self.numLb.text = numStr;    if (self.numBlock) {        self.numBlock(numStr);    }    }- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = UIColor.whiteColor;        [self loadSubV];    }    return self;}- (void)loadSubV {        [self.contentView addSubview:self.titleLb];    [self.contentView addSubview:self.addBt];    [self.contentView addSubview:self.minusBt];    [self.contentView addSubview:self.numLb];//    if(num >= self.goodsmaxNum){//        [self.addBtn setTitleColor:[UIColor colorWithHexString:@"#E6E6E6"] forState:UIControlStateNormal];//        self.addBtn.userInteractionEnabled = NO;//    }else{//        [self.addBtn setTitleColor:[UIColor colorWithHexString:@"#0B0B0B"] forState:UIControlStateNormal];//        self.addBtn.userInteractionEnabled = YES;//    }//    if(num >= 2){//        [self.cutBtn setTitleColor:[UIColor colorWithHexString:@"#0B0B0B"] forState:UIControlStateNormal];//        self.cutBtn.userInteractionEnabled = YES;//    }else{//        [self.cutBtn setTitleColor:[UIColor colorWithHexString:@"#E6E6E6"] forState:UIControlStateNormal];//        self.cutBtn.userInteractionEnabled = NO;//    }        [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {        make.height.mas_equalTo(20);        make.top.equalTo(self.contentView);        make.leading.equalTo(self.contentView);    }];        [self.minusBt mas_makeConstraints:^(MASConstraintMaker *make) {        make.width.height.mas_equalTo(32);        make.left.mas_equalTo(0);        make.top.equalTo(self.titleLb.mas_bottom).offset(10);        make.bottom.mas_equalTo(-10);    }];        [self.numLb mas_makeConstraints:^(MASConstraintMaker *make) {        make.height.equalTo(@32);        make.width.equalTo(@46);        make.left.equalTo(self.minusBt.mas_right).offset(-1);        make.centerY.equalTo(self.minusBt);    }];        [self.addBt mas_makeConstraints:^(MASConstraintMaker *make) {        make.width.height.equalTo(@32);        make.left.equalTo(self.numLb.mas_right).offset(-1);        make.centerY.equalTo(self.minusBt);    }];    }- (UILabel *)titleLb {    if (!_titleLb) {        UILabel *lb = [UILabel baseLb];        lb.textColor = Col_000;        lb.text = @"Quantity";        lb.font = [UIFont fontWithName:Rob_Bold size:16];        _titleLb = lb;    }    return _titleLb;}- (UIButton *)addBt {    if (!_addBt) {        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];        [bt setTitle:@"+" forState:UIControlStateNormal];        [bt setTitleColor:Col_000 forState:UIControlStateNormal];        bt.titleLabel.font = [UIFont fontWithName:Rob_Bold size:14];//        [bt setImage:[UIImage imageNamed:@"goods_num_add"] forState:UIControlStateNormal];        bt.layer.borderWidth = 1;        bt.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;        [bt addTarget:self action:@selector(addBtAction:) forControlEvents:UIControlEventTouchUpInside];        _addBt = bt;    }    return _addBt;}- (UILabel *)numLb {    if (!_numLb) {        UILabel *lb = [UILabel baseLb];        lb.textAlignment = NSTextAlignmentCenter;        lb.textColor = Col_000;        lb.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;        lb.layer.borderWidth = 1;        lb.text = @"1";        lb.font = [UIFont fontWithName:Rob_Bold size:16];        _numLb = lb;    }    return _numLb;}- (UIButton *)minusBt {    if (!_minusBt) {        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];        [bt setTitle:@"-" forState:UIControlStateNormal];        [bt setTitleColor:Col_000 forState:UIControlStateNormal];        bt.titleLabel.font = [UIFont fontWithName:Rob_Bold size:14];//        [bt setImage:[UIImage imageNamed:@"goods_num_minus"] forState:UIControlStateNormal];        bt.layer.borderWidth = 1;        bt.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;        [bt addTarget:self action:@selector(minusBtAction:) forControlEvents:UIControlEventTouchUpInside];        _minusBt = bt;    }    return _minusBt;}@end@interface APProductDetailSizeHeadV ()@end@implementation APProductDetailSizeHeadV- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = Col_FFF;        [self loadSubV];    }    return self;}- (void)loadSubV {    [self addSubview:self.titleLb];    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {        make.top.equalTo(self).offset(10);        make.bottom.equalTo(self);//.offset(-5);        make.leading.equalTo(self).offset(15);        make.trailing.equalTo(self).offset(-15);    }];}- (UILabel *)titleLb {    if (!_titleLb) {        UILabel *lb = [UILabel baseLb];        lb.textColor = Col_000;        lb.font = [UIFont fontWithName:Rob_Bold size:16];        _titleLb = lb;    }    return _titleLb;}@end
 |