CommodityDetailView.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // CommodityDetailView.m
  3. // yunbaolive
  4. //
  5. // Created by ybRRR on 2020/2/25.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "CommodityDetailView.h"
  9. #import "YBImageView.h"
  10. @implementation CommodityDetailView
  11. -(instancetype)initWithFrame:(CGRect)frame
  12. {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. self.backgroundColor = UIColor.whiteColor;
  16. imageArray = [NSMutableArray array];
  17. imgWidth = (_window_width - 75)/4;
  18. [self createUI];
  19. }
  20. return self;
  21. }
  22. -(void)createUI{
  23. UILabel*title = [[UILabel alloc]init];
  24. title.frame = CGRectMake(12, 10, 180, 20);
  25. title.font = [UIFont boldSystemFontOfSize:14];
  26. title.text = YZMsg(@"商品详情");
  27. title.textColor = [UIColor blackColor];
  28. [self addSubview:title];
  29. self.contentText = [[MyTextView alloc]init];
  30. self.contentText.frame = CGRectMake(12, title.bottom+10, _window_width-24, 180);
  31. self.contentText.font = [UIFont systemFontOfSize:14];
  32. self.contentText.placeholder = YZMsg(@"建议通过细节说明、规格、搭配、购买须知等多方面描述商品的详情,不超过300个字");
  33. self.contentText.textColor = [UIColor blackColor];
  34. self.contentText.backgroundColor = [UIColor clearColor];
  35. self.contentText.placeholderColor = [UIColor grayColor];
  36. self.contentText.returnKeyType = UIReturnKeyDone;
  37. self.contentText.delegate = self;
  38. [self addSubview:self.contentText];
  39. imgeBtn = [UIButton buttonWithType:0];
  40. imgeBtn.frame = CGRectMake(15, self.contentText.bottom+15, imgWidth, imgWidth);
  41. imgeBtn.backgroundColor = RGB(245, 245, 245);// RGB_COLOR(@"#302D41", 1);
  42. [imgeBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  43. imgeBtn.tag = 1000;
  44. [imgeBtn setImage:[UIImage imageNamed:@"addComm"] forState:0];
  45. [imgeBtn setTitle:YZMsg(@"详情图") forState:0];
  46. [imgeBtn setTitleColor:[UIColor grayColor] forState:0];
  47. imgeBtn.titleLabel.font = [UIFont systemFontOfSize:10];
  48. imgeBtn = [PublicObj setUpImgDownText:imgeBtn];
  49. [self addSubview:imgeBtn];
  50. }
  51. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  52. if ([text isEqualToString:@"\n"]) {
  53. [self endEditing:YES];
  54. return NO;//这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行
  55. }
  56. return YES;
  57. }
  58. - (void)imageBtnClick:(UIButton *)sender{
  59. TZImagePickerController *imagePC = [[TZImagePickerController alloc]initWithMaxImagesCount:20-imageArray.count delegate:self];
  60. imagePC.preferredLanguage = [lagType isEqual:ZH_CN] ? @"zh-Hans":@"en";
  61. imagePC.modalPresentationStyle = 0;
  62. imagePC.showSelectBtn = YES;
  63. imagePC.allowCrop = NO;
  64. imagePC.allowPickingOriginalPhoto = NO;
  65. imagePC.oKButtonTitleColorNormal = Pink_Cor;
  66. imagePC.allowTakePicture = YES;
  67. imagePC.allowTakeVideo = NO;
  68. imagePC.allowPickingVideo = NO;
  69. imagePC.allowPickingMultipleVideo = NO;
  70. [[[YBBaseAppDelegate sharedAppDelegate] topViewController]presentViewController:imagePC animated:YES completion:nil];
  71. }
  72. #pragma mark---------TZImagePickerControllerDelegate------------
  73. - (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
  74. NSLog(@"------多选择图片--:%@",photos);
  75. [imageArray addObjectsFromArray:photos];
  76. [self setImageFrame];
  77. [self.delegate setCommodityDetailImg:imageArray];
  78. }
  79. #pragma mark-------重新赋值图片坐标----------
  80. -(void)setImageFrame{
  81. for (UIButton *btn in self.subviews) {
  82. if (btn.tag >=3000) {
  83. [btn removeFromSuperview];
  84. }
  85. }
  86. //如果有图片
  87. if (imageArray.count > 0) {
  88. if (self.heightEvent) {
  89. self.heightEvent(330+ imageArray.count/4 *(imgWidth+15));
  90. }
  91. for (int i = 0; i < imageArray.count ; i ++) {
  92. UIButton *btn = [UIButton buttonWithType:0];
  93. btn.backgroundColor = RGB(250, 250, 250);
  94. btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
  95. btn.imageView.clipsToBounds = YES;
  96. btn.frame =CGRectMake(15+(i%4)*(imgWidth + 15), self.contentText.bottom+(i/4)*(imgWidth + 15), imgWidth, imgWidth);
  97. btn.tag = 3000+i;
  98. //如果是编辑图片需要赋值判断:判断图片类型:UIImage或网络图片
  99. if ([self.fromStr isEqual:@"seller"]) {
  100. if ([imageArray[i] isKindOfClass:[UIImage class]]) {
  101. [btn setImage:imageArray[i] forState:0];
  102. }else{
  103. [btn sd_setImageWithURL:[NSURL URLWithString:imageArray[i]] forState:0];
  104. }
  105. }else{
  106. [btn setImage:imageArray[i] forState:0];
  107. }
  108. [btn addTarget:self action:@selector(showImage:) forControlEvents:UIControlEventTouchUpInside];
  109. [self addSubview:btn];
  110. UIButton *delBtn = [UIButton buttonWithType:0];
  111. delBtn.frame = CGRectMake(imgWidth-25, 5, 20, 20);
  112. [delBtn setBackgroundImage:[UIImage imageNamed:@"comm_del"] forState:0];
  113. delBtn.tag = 4000+i;
  114. [delBtn addTarget:self action:@selector(delImageClick:) forControlEvents:UIControlEventTouchUpInside];
  115. [btn addSubview:delBtn];
  116. }
  117. }else{
  118. //没有图片
  119. self.heightEvent(330);
  120. imgeBtn.frame = CGRectMake(15, self.contentText.bottom+15, imgWidth, imgWidth);
  121. [imgeBtn setTitle:YZMsg(@"详情图") forState:0];
  122. return;
  123. }
  124. if (imageArray.count == 20) {
  125. imgeBtn.hidden = YES;
  126. }else{
  127. imgeBtn.hidden = NO;
  128. imgeBtn.frame = CGRectMake(15+(imageArray.count%4)*(imgWidth + 15), self.contentText.bottom+(imageArray.count/4)*(imgWidth + 15), imgWidth, imgWidth);
  129. [imgeBtn setTitle:[NSString stringWithFormat:@"%ld/20",imageArray.count] forState:0];
  130. imgeBtn = [PublicObj setUpImgDownText:imgeBtn];
  131. }
  132. }
  133. #pragma mark------预览图片---------------
  134. -(void)showImage:(UIButton *)sender{
  135. NSInteger index = sender.tag-3000;
  136. YBImageView *imgView = [[YBImageView alloc] initWithImageArray:imageArray andIndex:index andMine:NO isDtCell:NO andBlock:^(NSArray * _Nonnull array) {
  137. }];
  138. [imgView hideDelete];
  139. [[UIApplication sharedApplication].keyWindow addSubview:imgView];
  140. }
  141. #pragma mark------删除图片---------------
  142. -(void)delImageClick:(UIButton *)sender{
  143. NSInteger index = sender.tag-4000;
  144. [imageArray removeObjectAtIndex:index];
  145. [self setImageFrame];
  146. [self.delegate setCommodityDetailImg:imageArray];
  147. }
  148. #pragma mark--------编辑商品赋值-----------------
  149. -(void)setDetaiModelData:(CommodityDetailModel *)detailModel fromWhere:(NSString *)from
  150. {
  151. _goodsModel = detailModel;
  152. self.fromStr = from;
  153. //有图片
  154. if (detailModel.pictures_format.count > 0) {
  155. [imageArray addObjectsFromArray:detailModel.pictures_format];
  156. [self setImageFrame];
  157. [self.delegate setCommodityDetailImg:imageArray];
  158. }
  159. }
  160. @end