ASGoodsDetailsSizeSelView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 = 84;
  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 = 84;
  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. self.bgV.layer.borderWidth = 1;
  211. self.bgV.backgroundColor = _F8F8F8;
  212. self.bgV.layer.borderColor = isSel ? _32CFB0.CGColor : Col_FFF.CGColor;
  213. self.selTipV.hidden = !isSel;
  214. self.titleLb.font = [UIFont fontWithName:(isSel ? Rob_Bold : Rob_Regular ) size:(isCat ? 12 : 14)];
  215. }
  216. - (void)setTip:(NSString *)tipStr {
  217. self.selTipV.hidden = true;
  218. self.bgV.layer.borderWidth = 0;
  219. self.bgV.backgroundColor = UIColor.clearColor;
  220. self.colorBg.hidden = false;
  221. self.titleLb.text = tipStr;
  222. }
  223. - (void)loadSubV {
  224. [self.contentView addSubview:self.colorBg];
  225. [self.contentView addSubview:self.bgV];
  226. [self.colorBg mas_makeConstraints:^(MASConstraintMaker *make) {
  227. make.edges.equalTo(self.contentView);
  228. }];
  229. [self.bgV mas_makeConstraints:^(MASConstraintMaker *make) {
  230. make.edges.equalTo(self.contentView);
  231. }];
  232. [self.bgV addSubview:self.titleLb];
  233. [self.bgV addSubview:self.selTipV];
  234. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  235. make.top.leading.equalTo(self.bgV).offset(4);
  236. make.bottom.trailing.equalTo(self.bgV).offset(-4);
  237. }];
  238. [self.selTipV mas_makeConstraints:^(MASConstraintMaker *make) {
  239. make.trailing.bottom.equalTo(self.bgV);
  240. make.width.height.equalTo(@12);
  241. }];
  242. }
  243. - (void)layoutSubviews {
  244. [super layoutSubviews];
  245. dispatch_async(dispatch_get_main_queue(), ^{
  246. // [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)]];
  247. });
  248. }
  249. - (UIView *)bgV {
  250. if (!_bgV) {
  251. UIView *v = [UIView baseV];
  252. v.layer.borderWidth = 1;
  253. v.backgroundColor = Col_FFF;
  254. v.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
  255. _bgV = v;
  256. }
  257. return _bgV;
  258. }
  259. - (UILabel *)titleLb {
  260. if (!_titleLb) {
  261. UILabel *lb = [UILabel baseLb];
  262. lb.textAlignment = NSTextAlignmentCenter;
  263. lb.textColor = Col_000;
  264. lb.numberOfLines = 0;
  265. lb.font = [UIFont fontWithName:Rob_Regular size:14];
  266. _titleLb = lb;
  267. }
  268. return _titleLb;
  269. }
  270. - (UIImageView *)selTipV {
  271. if (!_selTipV) {
  272. UIImageView *imgv = [UIImageView baseImgV];
  273. imgv.contentMode = UIViewContentModeScaleToFill;
  274. imgv.image = [UIImage imageNamed:@"good_sizeSel"];
  275. _selTipV = imgv;
  276. }
  277. return _selTipV;
  278. }
  279. - (UIView *)colorBg {
  280. if (!_colorBg) {
  281. UIView *v = [UIView baseV];
  282. v.hidden = true;
  283. _colorBg = v;
  284. }
  285. return _colorBg;
  286. }
  287. @end
  288. @interface APProductDetailNumStepCell ()
  289. @property (nonatomic, strong) UILabel *titleLb;
  290. @property (nonatomic, strong) UIButton *minusBt;
  291. @property (nonatomic, strong) UILabel *numLb;
  292. @property (nonatomic, strong) UIButton *addBt;
  293. @end
  294. @implementation APProductDetailNumStepCell
  295. - (void)addBtAction:(UIButton *)bt {
  296. // [bt sleep:0.3];
  297. NSInteger num = self.numLb.text.integerValue;
  298. if (num < 1) {
  299. num = 1;
  300. } else {
  301. num += 1;
  302. }
  303. NSString *numStr = [NSString stringWithFormat:@"%ld", num];
  304. self.numLb.text = numStr;
  305. if (self.numBlock) {
  306. self.numBlock(numStr);
  307. }
  308. }
  309. - (void)minusBtAction:(UIButton *)bt {
  310. // [bt sleep:0.3];
  311. NSInteger num = self.numLb.text.integerValue;
  312. if (num < 2) {
  313. num = 1;
  314. } else {
  315. num -= 1;
  316. }
  317. NSString *numStr = [NSString stringWithFormat:@"%ld", num];
  318. self.numLb.text = numStr;
  319. if (self.numBlock) {
  320. self.numBlock(numStr);
  321. }
  322. }
  323. - (instancetype)initWithFrame:(CGRect)frame
  324. {
  325. self = [super initWithFrame:frame];
  326. if (self) {
  327. self.backgroundColor = UIColor.whiteColor;
  328. [self loadSubV];
  329. }
  330. return self;
  331. }
  332. - (void)loadSubV {
  333. [self.contentView addSubview:self.titleLb];
  334. [self.contentView addSubview:self.addBt];
  335. [self.contentView addSubview:self.minusBt];
  336. [self.contentView addSubview:self.numLb];
  337. // if(num >= self.goodsmaxNum){
  338. // [self.addBtn setTitleColor:[UIColor colorWithHexString:@"#E6E6E6"] forState:UIControlStateNormal];
  339. // self.addBtn.userInteractionEnabled = NO;
  340. // }else{
  341. // [self.addBtn setTitleColor:[UIColor colorWithHexString:@"#0B0B0B"] forState:UIControlStateNormal];
  342. // self.addBtn.userInteractionEnabled = YES;
  343. // }
  344. // if(num >= 2){
  345. // [self.cutBtn setTitleColor:[UIColor colorWithHexString:@"#0B0B0B"] forState:UIControlStateNormal];
  346. // self.cutBtn.userInteractionEnabled = YES;
  347. // }else{
  348. // [self.cutBtn setTitleColor:[UIColor colorWithHexString:@"#E6E6E6"] forState:UIControlStateNormal];
  349. // self.cutBtn.userInteractionEnabled = NO;
  350. // }
  351. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  352. make.height.mas_equalTo(20);
  353. make.top.equalTo(self.contentView);
  354. make.leading.equalTo(self.contentView);
  355. }];
  356. [self.minusBt mas_makeConstraints:^(MASConstraintMaker *make) {
  357. make.width.height.mas_equalTo(32);
  358. make.left.mas_equalTo(0);
  359. make.top.equalTo(self.titleLb.mas_bottom).offset(10);
  360. make.bottom.mas_equalTo(-10);
  361. }];
  362. [self.numLb mas_makeConstraints:^(MASConstraintMaker *make) {
  363. make.height.equalTo(@32);
  364. make.width.equalTo(@46);
  365. make.left.equalTo(self.minusBt.mas_right).offset(1);
  366. make.centerY.equalTo(self.minusBt);
  367. }];
  368. [self.addBt mas_makeConstraints:^(MASConstraintMaker *make) {
  369. make.width.height.equalTo(@32);
  370. make.left.equalTo(self.numLb.mas_right).offset(1);
  371. make.centerY.equalTo(self.minusBt);
  372. }];
  373. }
  374. - (UILabel *)titleLb {
  375. if (!_titleLb) {
  376. UILabel *lb = [UILabel baseLb];
  377. lb.textColor = Col_000;
  378. lb.text = @"Quantity";
  379. lb.font = [UIFont fontWithName:Rob_Bold size:16];
  380. _titleLb = lb;
  381. }
  382. return _titleLb;
  383. }
  384. - (UIButton *)addBt {
  385. if (!_addBt) {
  386. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  387. [bt setImage:[UIImage imageNamed:@"goods_num_add"] forState:UIControlStateNormal];
  388. bt.layer.borderWidth = 1;
  389. bt.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
  390. [bt addTarget:self action:@selector(addBtAction:) forControlEvents:UIControlEventTouchUpInside];
  391. _addBt = bt;
  392. }
  393. return _addBt;
  394. }
  395. - (UILabel *)numLb {
  396. if (!_numLb) {
  397. UILabel *lb = [UILabel baseLb];
  398. lb.textAlignment = NSTextAlignmentCenter;
  399. lb.textColor = Col_000;
  400. lb.layer.borderColor = Col_000.CGColor;
  401. lb.layer.borderWidth = 1;
  402. lb.text = @"1";
  403. lb.font = [UIFont fontWithName:Rob_Bold size:16];
  404. _numLb = lb;
  405. }
  406. return _numLb;
  407. }
  408. - (UIButton *)minusBt {
  409. if (!_minusBt) {
  410. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  411. [bt setImage:[UIImage imageNamed:@"goods_num_minus"] forState:UIControlStateNormal];
  412. bt.layer.borderWidth = 1;
  413. bt.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
  414. [bt addTarget:self action:@selector(minusBtAction:) forControlEvents:UIControlEventTouchUpInside];
  415. _minusBt = bt;
  416. }
  417. return _minusBt;
  418. }
  419. @end
  420. @interface APProductDetailSizeHeadV ()
  421. @end
  422. @implementation APProductDetailSizeHeadV
  423. - (instancetype)initWithFrame:(CGRect)frame
  424. {
  425. self = [super initWithFrame:frame];
  426. if (self) {
  427. self.backgroundColor = Col_FFF;
  428. [self loadSubV];
  429. }
  430. return self;
  431. }
  432. - (void)loadSubV {
  433. [self addSubview:self.titleLb];
  434. [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
  435. make.top.equalTo(self).offset(10);
  436. make.bottom.equalTo(self);//.offset(-5);
  437. make.leading.equalTo(self).offset(15);
  438. make.trailing.equalTo(self).offset(-15);
  439. }];
  440. }
  441. - (UILabel *)titleLb {
  442. if (!_titleLb) {
  443. UILabel *lb = [UILabel baseLb];
  444. lb.textColor = Col_000;
  445. lb.font = [UIFont fontWithName:Rob_Bold size:16];
  446. _titleLb = lb;
  447. }
  448. return _titleLb;
  449. }
  450. @end