HeaderView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // HeaderView.m
  3. // BuguLive
  4. //
  5. // Created by GuoMs on 16/8/5.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "HeaderView.h"
  9. #import "UIImageView+WebCache.h"
  10. #import "AuctionTagCell.h"
  11. #define BtnW 60
  12. #define BtnX 8
  13. #define BtnY 12
  14. #define BtnH 30
  15. #define Space 8
  16. #define imagW 90
  17. #define imagH 90
  18. @implementation HeaderView
  19. - (NSMutableArray *)cellcount
  20. {
  21. if (!_cellcount)
  22. {
  23. self.cellcount = [NSMutableArray new];
  24. }
  25. return _cellcount;
  26. }
  27. - (void)awakeFromNib
  28. {
  29. [super awakeFromNib];
  30. self.backgroundColor = kAppSpaceColor3;
  31. self.getBtnTypeScrollow.backgroundColor = kAppSpaceColor3;
  32. self.getBtnTypeScrollow.layer.borderWidth = 0.5f;
  33. self.getBtnTypeScrollow.layer.borderColor = [kAppGrayColor4 CGColor];
  34. self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
  35. self.flowLayout.itemSize = CGSizeMake(imagW + 10, imagH + 10);
  36. self.flowLayout.sectionInset = UIEdgeInsetsMake(0.5, 0.5, 0.5, 0.5);
  37. self.flowLayout.minimumLineSpacing = 2;
  38. self.flowLayout.minimumInteritemSpacing = 2;
  39. self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  40. self.getPhotoCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, self.bounds.size.height - 56) collectionViewLayout:self.flowLayout];
  41. self.getPhotoCollection.backgroundColor = kAppSpaceColor3;
  42. self.getPhotoCollection.showsVerticalScrollIndicator = NO;
  43. self.getPhotoCollection.showsHorizontalScrollIndicator = NO;
  44. self.getPhotoCollection.delegate = self;
  45. self.getPhotoCollection.dataSource = self;
  46. [self.getPhotoCollection registerNib:[UINib nibWithNibName:@"PhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"photoCell"];
  47. [self addSubview:self.getPhotoCollection];
  48. }
  49. - (void)setTagsArr:(NSArray *)tagsArr
  50. {
  51. _tagsArr = tagsArr;
  52. if (tagsArr.count > 0)
  53. {
  54. UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc] init] ;
  55. flow.minimumLineSpacing = 8;
  56. flow.minimumInteritemSpacing = 8;
  57. flow.sectionInset = UIEdgeInsetsMake(BtnY, 10, BtnY, 10);
  58. flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  59. self.tagsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.height-54, kScreenW, 54) collectionViewLayout:flow];
  60. self.tagsCollectionView.delegate = self;
  61. self.tagsCollectionView.dataSource = self;
  62. self.tagsCollectionView.backgroundColor = kAppSpaceColor3;
  63. self.tagsCollectionView.showsHorizontalScrollIndicator = NO;
  64. self.tagsCollectionView.showsVerticalScrollIndicator = NO;
  65. [self addSubview:self.tagsCollectionView];
  66. [self.tagsCollectionView registerNib:[UINib nibWithNibName:@"AuctionTagCell" bundle:nil] forCellWithReuseIdentifier:@"AuctionTagCell"];
  67. }
  68. }
  69. - (void)getPicturearr:(NSMutableArray *)arr block:(void (^)())block
  70. {
  71. self.cellcount = arr;
  72. [self.getPhotoCollection reloadData];
  73. }
  74. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  75. {
  76. if (collectionView == self.getPhotoCollection)
  77. {
  78. if (self.isOTOShop)
  79. {
  80. return 1;
  81. }
  82. else if (self.cellcount.count < 5)
  83. {
  84. return self.cellcount.count;
  85. }
  86. else
  87. {
  88. return 5;
  89. }
  90. }
  91. return self.tagsArr.count;
  92. }
  93. #pragma mark Falow LayOut Delegate
  94. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  95. {
  96. if (collectionView == self.getPhotoCollection)
  97. {
  98. return CGSizeMake( kScreenW / 4 + 10,kScreenW/4 + 10);
  99. }
  100. else if(collectionView == self.tagsCollectionView)
  101. {
  102. if (self.tagsArr.count > indexPath.item)
  103. {
  104. TagsModel * model = self.tagsArr[indexPath.item];
  105. CGSize titleSize = [model.name boundingRectWithSize:CGSizeMake(MAXFLOAT, BtnH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:kAppSmallTextFont} context:nil].size;
  106. return CGSizeMake(titleSize.width+20, BtnH) ;
  107. }
  108. }
  109. return CGSizeMake( 0, 0);
  110. }
  111. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  112. {
  113. if (collectionView == self.getPhotoCollection )
  114. {
  115. PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
  116. if (self.isOTOShop)
  117. {
  118. if (self.cellcount.count - 1 == indexPath.row)
  119. {
  120. //判断最后一张是否显示为增加图片的本地图片
  121. if ([self.cellcount.lastObject isEqual:@"me_addPhoto"])
  122. {
  123. cell.photoIMG.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.cellcount[indexPath.row]]];
  124. }
  125. else
  126. {
  127. [cell.photoIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.cellcount[indexPath.row]]] placeholderImage:nil];
  128. }
  129. }
  130. else
  131. {
  132. [cell.photoIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.cellcount[indexPath.row]]] placeholderImage:nil];
  133. }
  134. }
  135. else
  136. {
  137. if (indexPath.row == 0)
  138. {
  139. //判断最后一张是否显示为增加图片的本地图片
  140. if ([self.cellcount.firstObject isEqual:@"me_addPhoto"])
  141. {
  142. cell.photoIMG.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.cellcount[indexPath.row]]];
  143. }
  144. else
  145. {
  146. [cell.photoIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.cellcount[indexPath.row]]] placeholderImage:nil];
  147. }
  148. }
  149. else
  150. {
  151. [cell.photoIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.cellcount[indexPath.row]]] placeholderImage:nil];
  152. }
  153. }
  154. // if (self.cellcount.count - 1 == indexPath.row)
  155. // {
  156. // //判断最后一张是否显示为增加图片的本地图片
  157. // if ([self.cellcount.lastObject isEqual:@"me_addPhoto"])
  158. // {
  159. // cell.photoIMG.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.cellcount[indexPath.row]]];
  160. // }
  161. // else
  162. // {
  163. // [cell.photoIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.cellcount[indexPath.row]]] placeholderImage:nil];
  164. // }
  165. // }
  166. // else
  167. // {
  168. // [cell.photoIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.cellcount[indexPath.row]]] placeholderImage:nil];
  169. // }
  170. // UILongPressGestureRecognizer* longgs=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpress:)];
  171. // [cell addGestureRecognizer:longgs];//为cell添加手势
  172. // longgs.minimumPressDuration = 1.0;//定义长按识别时长
  173. // longgs.view.tag = indexPath.row;//将手势和cell的序号绑定
  174. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleToTap:)];
  175. cell.tag = indexPath.row;
  176. [cell.contentView addGestureRecognizer:tap];
  177. UITapGestureRecognizer *tapcancel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleToTaptapcancel:)];
  178. cell.cancelIMG.tag = indexPath.row;
  179. [cell.cancelIMG addGestureRecognizer:tapcancel];
  180. if (!_isOTOShop)
  181. {
  182. if (indexPath.row != 0 || ![self.cellcount.firstObject isEqual:@"me_addPhoto"])
  183. {
  184. cell.cancelIMG.hidden = NO;
  185. }
  186. else
  187. {
  188. cell.cancelIMG.hidden = YES;
  189. }
  190. }
  191. else
  192. {
  193. cell.cancelIMG.hidden = YES;
  194. }
  195. return cell;
  196. }
  197. else
  198. {
  199. AuctionTagCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AuctionTagCell" forIndexPath:indexPath];
  200. if (self.tagsArr.count > indexPath.item)
  201. {
  202. cell.model = self.tagsArr[indexPath.item];
  203. }
  204. return cell;
  205. }
  206. }
  207. - (void)longpress:(UILongPressGestureRecognizer*)ges
  208. {
  209. PhotoCollectionViewCell *cell = (PhotoCollectionViewCell*)ges.view;
  210. NSInteger row = ges.view.tag;
  211. if(ges.state==UIGestureRecognizerStateBegan && !_isOTOShop)
  212. {
  213. // if (self.cellcount.count - 1 != row)
  214. // {
  215. // cell.cancelIMG.hidden = NO;
  216. // }
  217. if (row != 0 || ![self.cellcount.firstObject isEqual:@"me_addPhoto"])
  218. {
  219. cell.cancelIMG.hidden = NO;
  220. }
  221. }
  222. }
  223. - (void)handleToTaptapcancel:(UITapGestureRecognizer *)tap
  224. {
  225. if (self.cellcount.count > 1)
  226. {
  227. NSInteger row = tap.view.tag;
  228. if (self.cellcount.count == 5 && ![self.cellcount.firstObject isEqual:@"me_addPhoto"])
  229. {
  230. [self.cellcount removeObjectAtIndex:row];
  231. [self.cellcount insertObject:@"me_addPhoto" atIndex:0];
  232. self.getPhotoCollection.contentOffset = CGPointMake(0, 0);
  233. }
  234. else
  235. {
  236. [self.cellcount removeObjectAtIndex:row];
  237. }
  238. [self.getPhotoCollection reloadData];
  239. }
  240. }
  241. #pragma mark -- 照片的点击事件
  242. - (void)handleToTap:(UITapGestureRecognizer *)tap
  243. {
  244. if ([self.delegate respondsToSelector:@selector(handleToTapPhoto:)])
  245. {
  246. [self.delegate handleToTapPhoto:tap];
  247. }
  248. }
  249. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  250. {
  251. if (self.tagsCollectionView == collectionView)
  252. {
  253. for (TagsModel * model in self.tagsArr)
  254. {
  255. model.isSelected = NO;
  256. }
  257. if (self.tagsArr.count > indexPath.item)
  258. {
  259. TagsModel * selectModle = self.tagsArr[indexPath.item];
  260. selectModle.isSelected = YES;
  261. if ([self.delegate respondsToSelector:@selector(handleWithGoodsTag:)])
  262. {
  263. [self.delegate handleWithGoodsTag:selectModle.name];
  264. }
  265. }
  266. [self.tagsCollectionView reloadData];
  267. }
  268. }
  269. @end