ASGoodsDetailsSizeSelView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //
  2. // ASGoodsDetailsSizeSelView.m
  3. // westkissMob
  4. //
  5. // Created by iOS on 2024/5/19.
  6. //
  7. #import "ASGoodsDetailsSizeSelView.h"
  8. @interface ASGoodsDetailsSizeSelView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  9. @end
  10. @implementation ASGoodsDetailsSizeSelView
  11. -(void)setTipStr:(NSString *)tipStr {
  12. _tipStr = tipStr;
  13. CGFloat h = 72;
  14. for (OptionsModel *m in self.optionArr) {
  15. if (m.isLongValue) {
  16. if (m.values.count > 0) {
  17. NSInteger numLine = m.values.count/2 + (m.values.count%2>0 ? 1 : 0);
  18. h += 40 * numLine + 10*(numLine-1) + 20 + 28;
  19. }
  20. } else {
  21. if (m.values.count > 0) {
  22. NSInteger numLine = m.values.count/4 + (m.values.count%4>0 ? 1 : 0);
  23. h += 40 * numLine + 10*(numLine-1) + 20+28;
  24. }
  25. }
  26. }
  27. // if (self.tipStr != nil && self.tipStr.length > 0) {
  28. // h += 60;
  29. // }
  30. if (self.reloadHeight) {
  31. self.reloadHeight(h);
  32. }
  33. if (self.reloadPrice){
  34. self.reloadPrice();
  35. }
  36. [self reloadData];
  37. }
  38. - (void)setOptionArr:(NSArray<OptionsModel *> *)optionArr {
  39. _optionArr = optionArr;
  40. CGFloat h = 72;
  41. for (OptionsModel *m in optionArr) {
  42. if (m.isLongValue) {
  43. if (m.values.count > 0) {
  44. NSInteger numLine = m.values.count/2 + (m.values.count%2>0 ? 1 : 0);
  45. h += 40 * numLine + 10*(numLine-1) + 20 + 28;
  46. }
  47. } else {
  48. if (m.values.count > 0) {
  49. NSInteger numLine = m.values.count/4 + (m.values.count%4>0 ? 1 : 0);
  50. h += 40 * numLine + 10*(numLine-1) + 20+28;
  51. }
  52. }
  53. }
  54. // if (self.tipStr != nil && self.tipStr.length > 0) {
  55. // h += 60;
  56. // }
  57. if (self.reloadHeight) {
  58. self.reloadHeight(h);
  59. }
  60. if (self.reloadPrice){
  61. self.reloadPrice();
  62. }
  63. [self reloadData];
  64. }
  65. - (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
  66. UICollectionViewFlowLayout *ly = [[UICollectionViewFlowLayout alloc] init];
  67. ly.scrollDirection = UICollectionViewScrollDirectionVertical;
  68. self = [super initWithFrame:frame collectionViewLayout:ly];
  69. if (self) {
  70. self.num = 1;
  71. self.showsVerticalScrollIndicator = false;
  72. self.showsHorizontalScrollIndicator = false;
  73. self.scrollEnabled = false;
  74. self.backgroundColor = Col_FFF;
  75. self.delegate = self;
  76. self.dataSource = self;
  77. [self registCell];
  78. [self reloadData];
  79. }
  80. return self;
  81. }
  82. - (void)registCell {
  83. [self registerClass:[APProductDetailSizeHeadV class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"APProductDetailSizeHeadV"];
  84. [self registerClass:[APProductDetailSizeCell class] forCellWithReuseIdentifier:@"APProductDetailSizeCell"];
  85. // [self registerClass:[APProductDetailSizeCell class] forCellWithReuseIdentifier:@"tipCell"];
  86. [self registerClass:[APProductDetailNumStepCell class] forCellWithReuseIdentifier:@"APProductDetailNumStepCell"];
  87. }
  88. // MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  89. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  90. if (indexPath.section < self.optionArr.count) {
  91. OptionsModel *m = self.optionArr[indexPath.section];
  92. if ((!m.is_require.boolValue) && indexPath.row == m.optionSelectTag) {
  93. m.optionSelectTag = -1;
  94. if (self.reloadPrice) {
  95. self.reloadPrice();
  96. }
  97. [collectionView reloadData];
  98. return;
  99. }
  100. if (m.values.count > indexPath.row) {
  101. m.optionSelectTag = indexPath.row;
  102. if (self.reloadPrice) {
  103. self.reloadPrice();
  104. }
  105. [collectionView reloadData];
  106. }
  107. }
  108. }
  109. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  110. // return self.optionArr.count + 1 + (self.tipStr == nil || self.tipStr.length == 0 ? 0 : 1);
  111. return self.optionArr.count + 1;
  112. }
  113. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  114. if (section < self.optionArr.count) {
  115. OptionsModel *m = self.optionArr[section];
  116. return m.values.count;
  117. }
  118. return 1;
  119. }
  120. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  121. if (indexPath.section < self.optionArr.count) {
  122. OptionsModel *m = self.optionArr[indexPath.section];
  123. APProductDetailSizeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"APProductDetailSizeCell" forIndexPath:indexPath];
  124. if (indexPath.row < m.values.count) {
  125. OptionsValuesM *val = m.values[indexPath.row];
  126. [cell setData:val];
  127. }
  128. [cell setSelType:m.optionSelectTag == indexPath.row isCat:m.isLongValue];
  129. return cell;
  130. }
  131. // if (self.tipStr != nil && self.tipStr.length > 0 && indexPath.section == self.optionArr.count) {
  132. // APProductDetailSizeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"APProductDetailSizeCell" forIndexPath:indexPath];
  133. // [cell setTip:self.tipStr];
  134. // return cell;
  135. // }
  136. APProductDetailNumStepCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"APProductDetailNumStepCell" forIndexPath:indexPath];
  137. __weak typeof(self) weakSelf = self;
  138. cell.numBlock = ^(NSString * _Nonnull num) {
  139. weakSelf.num = num.integerValue;
  140. if (self.reloadPrice) {
  141. self.reloadPrice();
  142. }
  143. };
  144. return cell;
  145. }
  146. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  147. if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  148. APProductDetailSizeHeadV *v = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"APProductDetailSizeHeadV" forIndexPath:indexPath];
  149. if (indexPath.section < self.optionArr.count) {
  150. OptionsModel *m = self.optionArr[indexPath.section];
  151. v.titleLb.text = m.title;
  152. } else {
  153. v.titleLb.text = @"Category";
  154. }
  155. return v;
  156. }
  157. return [[UICollectionReusableView alloc] init];
  158. }
  159. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  160. if (indexPath.section < self.optionArr.count) {
  161. OptionsModel *m = self.optionArr[indexPath.section];
  162. if (m.isLongValue) {
  163. CGFloat w = (SCREEN_WIDTH-30-10)/2;
  164. return CGSizeMake(w, 40);
  165. } else {
  166. CGFloat w = (SCREEN_WIDTH-30-10*3)/4;
  167. return CGSizeMake(w, 40);
  168. }
  169. } else {
  170. CGFloat w = (SCREEN_WIDTH-30);
  171. return CGSizeMake(w, 72);
  172. }
  173. }
  174. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  175. if (section >= self.optionArr.count){
  176. return CGSizeZero;
  177. }
  178. return CGSizeMake(SCREEN_WIDTH-30, 28);
  179. }
  180. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  181. return 10;
  182. }
  183. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  184. return 10;
  185. }
  186. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  187. return UIEdgeInsetsMake(10, 15, 10, 15);
  188. }
  189. @end
  190. @interface APProductDetailSizeCell ()
  191. @property (nonatomic, strong) UIView *bgV;
  192. @property (nonatomic, strong) UIView *colorBg;
  193. @property (nonatomic, strong) UILabel *titleLb;
  194. @property (nonatomic, strong) UIImageView *selTipV;
  195. @end
  196. @implementation APProductDetailSizeCell
  197. - (void)setData:(OptionsValuesM *)m {
  198. self.titleLb.text = m.title;
  199. }
  200. - (instancetype)initWithFrame:(CGRect)frame
  201. {
  202. self = [super initWithFrame:frame];
  203. if (self) {
  204. [self loadSubV];
  205. }
  206. return self;
  207. }
  208. - (void)setSelType:(BOOL)isSel isCat:(BOOL)isCat {
  209. self.colorBg.hidden = true;
  210. TT_ViewRadius(self.bgV, 4);
  211. self.bgV.layer.borderWidth = 1;
  212. self.bgV.backgroundColor = isSel ? _32CFB0 : Col_FFF;
  213. self.bgV.layer.borderColor = isSel ? _32CFB0.CGColor : [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
  214. self.selTipV.hidden = !isSel;
  215. self.titleLb.font = [UIFont fontWithName:(isSel ? Rob_Bold : Rob_Regular ) size:(isCat ? 12 : 14)];
  216. }
  217. - (void)setTip:(NSString *)tipStr {
  218. self.selTipV.hidden = true;
  219. self.bgV.layer.borderWidth = 0;
  220. self.bgV.backgroundColor = UIColor.clearColor;
  221. self.colorBg.hidden = false;
  222. self.titleLb.text = tipStr;
  223. }
  224. - (void)loadSubV {
  225. [self.contentView addSubview:self.colorBg];
  226. [self.contentView addSubview:self.bgV];
  227. [self.colorBg mas_makeConstraints:^(MASConstraintMaker *make) {
  228. make.edges.equalTo(self.contentView);
  229. }];
  230. [self.bgV mas_makeConstraints:^(MASConstraintMaker *make) {
  231. make.edges.equalTo(self.contentView);
  232. }];
  233. [self.bgV addSubview:self.titleLb];
  234. [self.bgV addSubview:self.selTipV];
  235. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  236. make.top.leading.equalTo(self.bgV).offset(4);
  237. make.bottom.trailing.equalTo(self.bgV).offset(-4);
  238. }];
  239. [self.selTipV mas_makeConstraints:^(MASConstraintMaker *make) {
  240. make.trailing.bottom.equalTo(self.bgV);
  241. make.width.height.equalTo(@12);
  242. }];
  243. }
  244. - (void)layoutSubviews {
  245. [super layoutSubviews];
  246. dispatch_async(dispatch_get_main_queue(), ^{
  247. // [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)]];
  248. });
  249. }
  250. - (UIView *)bgV {
  251. if (!_bgV) {
  252. UIView *v = [UIView baseV];
  253. v.layer.borderWidth = 1;
  254. v.backgroundColor = Col_FFF;
  255. v.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
  256. _bgV = v;
  257. }
  258. return _bgV;
  259. }
  260. - (UILabel *)titleLb {
  261. if (!_titleLb) {
  262. UILabel *lb = [UILabel baseLb];
  263. lb.textAlignment = NSTextAlignmentCenter;
  264. lb.textColor = Col_000;
  265. lb.numberOfLines = 0;
  266. lb.font = [UIFont fontWithName:Rob_Regular size:14];
  267. _titleLb = lb;
  268. }
  269. return _titleLb;
  270. }
  271. - (UIImageView *)selTipV {
  272. if (!_selTipV) {
  273. UIImageView *imgv = [UIImageView baseImgV];
  274. imgv.contentMode = UIViewContentModeScaleToFill;
  275. imgv.image = [UIImage imageNamed:@"good_sizeSel"];
  276. _selTipV = imgv;
  277. }
  278. return _selTipV;
  279. }
  280. - (UIView *)colorBg {
  281. if (!_colorBg) {
  282. UIView *v = [UIView baseV];
  283. v.hidden = true;
  284. _colorBg = v;
  285. }
  286. return _colorBg;
  287. }
  288. @end
  289. @interface APProductDetailNumStepCell ()
  290. @property (nonatomic, strong) UILabel *titleLb;
  291. @property (nonatomic, strong) UIButton *minusBt;
  292. @property (nonatomic, strong) UILabel *numLb;
  293. @property (nonatomic, strong) UIButton *addBt;
  294. @end
  295. @implementation APProductDetailNumStepCell
  296. - (void)addBtAction:(UIButton *)bt {
  297. // [bt sleep:0.3];
  298. NSInteger num = self.numLb.text.integerValue;
  299. if (num < 1) {
  300. num = 1;
  301. } else {
  302. num += 1;
  303. }
  304. NSString *numStr = [NSString stringWithFormat:@"%ld", num];
  305. self.numLb.text = numStr;
  306. if (self.numBlock) {
  307. self.numBlock(numStr);
  308. }
  309. }
  310. - (void)minusBtAction:(UIButton *)bt {
  311. // [bt sleep:0.3];
  312. NSInteger num = self.numLb.text.integerValue;
  313. if (num < 2) {
  314. num = 1;
  315. } else {
  316. num -= 1;
  317. }
  318. NSString *numStr = [NSString stringWithFormat:@"%ld", num];
  319. self.numLb.text = numStr;
  320. if (self.numBlock) {
  321. self.numBlock(numStr);
  322. }
  323. }
  324. - (instancetype)initWithFrame:(CGRect)frame
  325. {
  326. self = [super initWithFrame:frame];
  327. if (self) {
  328. self.backgroundColor = UIColor.whiteColor;
  329. [self loadSubV];
  330. }
  331. return self;
  332. }
  333. - (void)loadSubV {
  334. [self.contentView addSubview:self.titleLb];
  335. [self.contentView addSubview:self.addBt];
  336. [self.contentView addSubview:self.minusBt];
  337. [self.contentView addSubview:self.numLb];
  338. // if(num >= self.goodsmaxNum){
  339. // [self.addBtn setTitleColor:[UIColor colorWithHexString:@"#E6E6E6"] forState:UIControlStateNormal];
  340. // self.addBtn.userInteractionEnabled = NO;
  341. // }else{
  342. // [self.addBtn setTitleColor:[UIColor colorWithHexString:@"#0B0B0B"] forState:UIControlStateNormal];
  343. // self.addBtn.userInteractionEnabled = YES;
  344. // }
  345. // if(num >= 2){
  346. // [self.cutBtn setTitleColor:[UIColor colorWithHexString:@"#0B0B0B"] forState:UIControlStateNormal];
  347. // self.cutBtn.userInteractionEnabled = YES;
  348. // }else{
  349. // [self.cutBtn setTitleColor:[UIColor colorWithHexString:@"#E6E6E6"] forState:UIControlStateNormal];
  350. // self.cutBtn.userInteractionEnabled = NO;
  351. // }
  352. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  353. make.height.mas_equalTo(20);
  354. make.top.equalTo(self.contentView);
  355. make.leading.equalTo(self.contentView);
  356. }];
  357. [self.minusBt mas_makeConstraints:^(MASConstraintMaker *make) {
  358. make.width.height.mas_equalTo(32);
  359. make.left.mas_equalTo(0);
  360. make.top.equalTo(self.titleLb.mas_bottom).offset(10);
  361. make.bottom.mas_equalTo(-10);
  362. }];
  363. [self.numLb mas_makeConstraints:^(MASConstraintMaker *make) {
  364. make.height.equalTo(@32);
  365. make.width.equalTo(@46);
  366. make.left.equalTo(self.minusBt.mas_right).offset(-1);
  367. make.centerY.equalTo(self.minusBt);
  368. }];
  369. [self.addBt mas_makeConstraints:^(MASConstraintMaker *make) {
  370. make.width.height.equalTo(@32);
  371. make.left.equalTo(self.numLb.mas_right).offset(-1);
  372. make.centerY.equalTo(self.minusBt);
  373. }];
  374. }
  375. - (UILabel *)titleLb {
  376. if (!_titleLb) {
  377. UILabel *lb = [UILabel baseLb];
  378. lb.textColor = Col_000;
  379. lb.text = @"Quantity";
  380. lb.font = [UIFont fontWithName:Rob_Bold size:16];
  381. _titleLb = lb;
  382. }
  383. return _titleLb;
  384. }
  385. - (UIButton *)addBt {
  386. if (!_addBt) {
  387. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  388. [bt setTitle:@"+" forState:UIControlStateNormal];
  389. [bt setTitleColor:Col_000 forState:UIControlStateNormal];
  390. bt.titleLabel.font = [UIFont fontWithName:Rob_Bold size:14];
  391. // [bt setImage:[UIImage imageNamed:@"goods_num_add"] forState:UIControlStateNormal];
  392. bt.layer.borderWidth = 1;
  393. bt.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
  394. [bt addTarget:self action:@selector(addBtAction:) forControlEvents:UIControlEventTouchUpInside];
  395. _addBt = bt;
  396. }
  397. return _addBt;
  398. }
  399. - (UILabel *)numLb {
  400. if (!_numLb) {
  401. UILabel *lb = [UILabel baseLb];
  402. lb.textAlignment = NSTextAlignmentCenter;
  403. lb.textColor = Col_000;
  404. lb.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
  405. lb.layer.borderWidth = 1;
  406. lb.text = @"1";
  407. lb.font = [UIFont fontWithName:Rob_Bold size:16];
  408. _numLb = lb;
  409. }
  410. return _numLb;
  411. }
  412. - (UIButton *)minusBt {
  413. if (!_minusBt) {
  414. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  415. [bt setTitle:@"-" forState:UIControlStateNormal];
  416. [bt setTitleColor:Col_000 forState:UIControlStateNormal];
  417. bt.titleLabel.font = [UIFont fontWithName:Rob_Bold size:14];
  418. // [bt setImage:[UIImage imageNamed:@"goods_num_minus"] forState:UIControlStateNormal];
  419. bt.layer.borderWidth = 1;
  420. bt.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
  421. [bt addTarget:self action:@selector(minusBtAction:) forControlEvents:UIControlEventTouchUpInside];
  422. _minusBt = bt;
  423. }
  424. return _minusBt;
  425. }
  426. @end
  427. @interface APProductDetailSizeHeadV ()
  428. @end
  429. @implementation APProductDetailSizeHeadV
  430. - (instancetype)initWithFrame:(CGRect)frame
  431. {
  432. self = [super initWithFrame:frame];
  433. if (self) {
  434. self.backgroundColor = Col_FFF;
  435. [self loadSubV];
  436. }
  437. return self;
  438. }
  439. - (void)loadSubV {
  440. [self addSubview:self.titleLb];
  441. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  442. make.top.equalTo(self).offset(10);
  443. make.bottom.equalTo(self);//.offset(-5);
  444. make.leading.equalTo(self).offset(15);
  445. make.trailing.equalTo(self).offset(-15);
  446. }];
  447. }
  448. - (UILabel *)titleLb {
  449. if (!_titleLb) {
  450. UILabel *lb = [UILabel baseLb];
  451. lb.textColor = Col_000;
  452. lb.font = [UIFont fontWithName:Rob_Bold size:16];
  453. _titleLb = lb;
  454. }
  455. return _titleLb;
  456. }
  457. @end