WorkFLowCollectionSelectView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // WorkFLowCollectionSelectView.m
  3. // collectioview
  4. //
  5. // Created by Wenzhe Fan on 2019/11/7.
  6. // Copyright © 2019年 Wenzhe Fan. All rights reserved.
  7. //
  8. #import "WorkFLowCollectionSelectView.h"
  9. #import "BYHLabelsLayout.h"
  10. #define Xrang [ UIScreen mainScreen ].bounds.size.width/375 //屏幕宽比例
  11. #define Yrang [ UIScreen mainScreen ].bounds.size.height/667//屏幕高比例
  12. #define textSeleColor_work UIColorHex(#3979D3);
  13. #define backSeleColor_work UIColorHex(E6F0FD);
  14. #define textColor_work UIColorHex(999999);
  15. #define backColor_work UIColorHex(F7F7F7);
  16. //#define height self.frame.size.height * 0.8;
  17. @interface WorkFLowCollectionSelectView()<UICollectionViewDelegate, UICollectionViewDataSource, BYHLabelsLayoutDelegate>
  18. @property (nonatomic, strong) NSIndexPath *currentIndex;
  19. @property (strong,nonatomic) BYHLabelsLayout * layout;
  20. @property CGSize collviewSize;
  21. @property (nonatomic, strong) NSMutableArray *selectedType;
  22. @end
  23. @implementation WorkFLowCollectionSelectView
  24. //@synthesize approvalNameArray = _approvalNameArray;
  25. static WorkFLowCollectionSelectView * selectView;
  26. - (instancetype)init{
  27. self = [super init];
  28. if (self) {
  29. [self setFrame:[UIScreen mainScreen].bounds];
  30. [self cusView];
  31. }
  32. return self;
  33. }
  34. - (void)cusView
  35. {
  36. self.backgroundColor = [UIColorHex(#000000) colorWithAlphaComponent:0.3];
  37. [self addSubview:self.collectionView];
  38. CGFloat with = kGXScreenWidth - 80;
  39. CGFloat height = 480;
  40. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  41. make.center.mas_equalTo(self);
  42. make.size.mas_offset(CGSizeMake(with, height));
  43. }];
  44. self.collectionView.backgroundColor = UIColorHex(#FFFFFF);
  45. }
  46. #pragma mark - setter
  47. - (BYHLabelsLayout *)layout{
  48. if (!_layout) {
  49. _layout = [[BYHLabelsLayout alloc] init];
  50. _layout.delegate = self;
  51. }
  52. return _layout;
  53. }
  54. - (UICollectionView *)collectionView{
  55. if (!_collectionView) {
  56. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
  57. _collectionView.delegate = self;
  58. _collectionView.dataSource = self;
  59. _collectionView.layer.cornerRadius = 5;
  60. _collectionView.showsVerticalScrollIndicator = NO;
  61. _collectionView.showsHorizontalScrollIndicator = NO;
  62. _collectionView.layer.masksToBounds = YES;
  63. [_collectionView registerClass:[WorkFlowCollectViewCell class] forCellWithReuseIdentifier:@"Cell"];
  64. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
  65. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
  66. }
  67. return _collectionView;
  68. }
  69. - (NSMutableArray<WorkFilterModel *> *)selectedType{
  70. if(!_selectedType){
  71. _selectedType = [NSMutableArray array];
  72. }
  73. return _selectedType;
  74. }
  75. //- (void)setApprovalNameArray:(NSMutableArray<WorkFilterModel *> *)approvalNameArray {
  76. // _approvalNameArray = approvalNameArray;
  77. // //重新计算高度
  78. //
  79. //}
  80. - (WorkFilterModel *)getSelectedWithName:(NSString *)name {
  81. for (WorkFilterModel *model in self.selectedType) {
  82. if ([model.Label isEqualToString:name]) {
  83. return model;
  84. }
  85. }
  86. return nil;
  87. }
  88. - (NSIndexPath *)currentIndex{
  89. if (!_currentIndex) {
  90. _currentIndex = [[NSIndexPath alloc] init];
  91. }
  92. return _currentIndex;
  93. }
  94. - (void)collectionViewReloadData
  95. {
  96. [self show];
  97. [self.collectionView reloadData];
  98. self.collectionView.scrollEnabled = YES;
  99. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  100. [self.collectionView scrollToBottomAnimated:NO];
  101. self.collectionView.scrollEnabled = NO;
  102. });
  103. }
  104. - (void)show
  105. {
  106. [self.selectedType removeAllObjects];
  107. for (WorkFilterModel * model in self.approvalNameArray) {
  108. if (model.isSelected) {
  109. [self.selectedType addObject:model];
  110. }
  111. }
  112. [[UIApplication sharedApplication].keyWindow addSubview:self];
  113. [UIView animateWithDuration:0.1 animations:^{
  114. } completion:^(BOOL finished) {}];
  115. }
  116. - (void)dismiss
  117. {
  118. WS(weakSelf);
  119. [UIView animateWithDuration:0.1 animations:^{
  120. } completion:^(BOOL finished) {
  121. [weakSelf removeFromSuperview];
  122. }];
  123. }
  124. #pragma mark - 代理
  125. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  126. return 1;
  127. }
  128. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  129. return self.approvalNameArray.count;
  130. }
  131. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  132. WorkFlowCollectViewCell *cell = (WorkFlowCollectViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  133. NSArray *cellArray = [collectionView visibleCells];
  134. WorkFilterModel * fmodel = self.approvalNameArray.firstObject;
  135. WorkFilterModel * model = self.approvalNameArray[indexPath.item];
  136. model.isSelected = !model.isSelected;
  137. if (model.isSelected) {
  138. if (indexPath.item == 0) {
  139. [self.selectedType removeAllObjects];
  140. for (WorkFilterModel * smodel in self.approvalNameArray) {
  141. smodel.isSelected = NO;
  142. }
  143. for (WorkFlowCollectViewCell * celltem in cellArray) {
  144. [celltem selectNamel:NO];
  145. }
  146. model.isSelected = YES;
  147. [cell selectNamel:model.isSelected];
  148. [self.selectedType addObject:fmodel];
  149. }else{
  150. if (fmodel.isSelected) {
  151. WorkFlowCollectViewCell * celltem = (WorkFlowCollectViewCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
  152. fmodel.isSelected = NO;
  153. [celltem selectNamel:NO];
  154. [self.selectedType removeObject:fmodel];
  155. }
  156. [cell selectNamel:YES];
  157. [self.selectedType addObject:model];
  158. }
  159. }else{
  160. [cell selectNamel:model.isSelected];
  161. [self.selectedType removeObject:model];
  162. }
  163. // if ([self getSelectedWithName:self.approvalNameArray[indexPath.item].Label]) {
  164. // [self.selectedType removeObject:self.approvalNameArray[indexPath.item]];
  165. // [cell selectNamel:NO];
  166. // }else {
  167. // if (indexPath.item == 0) {
  168. // NSArray *cellArray = [collectionView visibleCells];
  169. // [self.selectedType removeAllObjects];
  170. // for (WorkFlowCollectViewCell * celltem in cellArray) {
  171. // [celltem selectNamel:NO];
  172. // }
  173. // }else{
  174. // WorkFlowCollectViewCell * celltem = (WorkFlowCollectViewCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
  175. // if (celltem.isSelected) {
  176. // [celltem selectNamel:NO];
  177. // [self.selectedType removeAllObjects];
  178. // }
  179. // }
  180. // [cell selectNamel:YES];
  181. // [self.selectedType addObject:self.approvalNameArray[indexPath.item]];
  182. // }
  183. }
  184. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
  185. // WorkFlowCollectViewCell * cell = (WorkFlowCollectViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  186. // [cell setDefaultColor];
  187. }
  188. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  189. WorkFlowCollectViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
  190. WorkFilterModel * model = self.approvalNameArray[indexPath.item];
  191. cell.namelabel.text = model.Label;
  192. //取消默认选中
  193. // if (indexPath.item == 0) {
  194. // [cell setSelectColor];
  195. // }else{
  196. [cell selectNamel:model.isSelected];
  197. //}
  198. //cell.namelabel.backgroundColor = [UIColor blackColor];
  199. return cell;
  200. }
  201. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  202. if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  203. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
  204. // headerView.backgroundColor = [UIColor blueColor];
  205. UILabel *lablel = [[UILabel alloc]initWithFrame:CGRectMake(15,15, 8.5, 8.5)];
  206. lablel.backgroundColor = UIColorHex(#3979D3);
  207. lablel.layer.cornerRadius = 4.25;
  208. lablel.layer.masksToBounds = YES;
  209. [headerView addSubview:lablel];
  210. UILabel *lableName = [UILabel new];
  211. lableName.text = @"类型";
  212. lableName.font = [UIFont systemFontOfSize:14.f];
  213. CGSize lableSize = [lableName sizeThatFits:CGSizeZero];
  214. lableName.frame = CGRectMake(28,11.75 ,lableSize.width,lableSize.height);
  215. [headerView addSubview:lableName];
  216. return headerView;
  217. }else{
  218. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
  219. UIButton *okButton = [UIButton buttonWithType:UIButtonTypeCustom];
  220. [okButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchDown];
  221. [okButton setTitle:@"确定" forState:UIControlStateNormal];
  222. [okButton setTitleColor:UIColorHex(#FFFFFF) forState:UIControlStateNormal];
  223. okButton.backgroundColor = UIColorHex(#5192EE);
  224. okButton.tag = 100;
  225. [self setButton:okButton];
  226. [footerView addSubview:okButton];
  227. CGFloat mywidt = collectionView.width - 38;
  228. CGFloat myhight = 36;
  229. okButton.frame = CGRectMake(19,0, mywidt, myhight);
  230. UIButton *okButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
  231. [okButton2 addTarget:self action:@selector(didClickButton2:) forControlEvents:UIControlEventTouchDown];
  232. [okButton2 setTitle:@"取消" forState:UIControlStateNormal];
  233. [okButton2 setTitleColor:UIColorHex(#999999) forState:UIControlStateNormal];
  234. okButton2.backgroundColor = UIColorHex(#F7F7F7);
  235. okButton2.frame = CGRectMake(19,36+15, mywidt, myhight);
  236. [self setButton:okButton2];
  237. okButton2.tag = 101;
  238. [footerView addSubview:okButton2];
  239. return footerView;
  240. }
  241. }
  242. - (void)setButton:(UIButton *)sender{
  243. sender.layer.cornerRadius = 5;
  244. sender.layer.masksToBounds = YES;
  245. [[sender titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  246. }
  247. - (void)didClickButton2:(UIButton *)sender{
  248. [self dismiss];
  249. }
  250. - (void)didClickButton:(UIButton *)sender{
  251. if ([self.delegate respondsToSelector:@selector(selecetWorkFLow:withModel:)]) {
  252. [self.delegate selecetWorkFLow:self.currentIndex withModel:self.selectedType];
  253. }
  254. [self dismiss];
  255. }
  256. #pragma mark - BYHLabelsLayoutDelegate
  257. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  258. CGFloat width = (collectionView.frame.size.width - 30 - 27)/2;
  259. CGFloat height = 32;
  260. if (indexPath.row == 0) {
  261. return CGSizeMake(width, height);
  262. }else if (indexPath.row == 1) {
  263. return CGSizeMake(width, height);
  264. }else if (indexPath.row == 2) {
  265. return CGSizeMake(width, height);
  266. }else if (indexPath.row == 3) {
  267. return CGSizeMake(width, height);
  268. }
  269. return CGSizeMake(width, height);
  270. }
  271. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  272. return 27;
  273. }
  274. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  275. return 15;
  276. }
  277. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout insetForSectionAtIndex:(NSInteger)section {
  278. return UIEdgeInsetsMake(0, 15, 20, 15);
  279. }
  280. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout referenceHeightForHeaderInSection:(NSInteger)section {
  281. if (section ==0) {
  282. return 40;
  283. }else{
  284. return 0;
  285. }
  286. }
  287. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout referenceHeightForFooterInSection:(NSInteger)section{
  288. if (section ==0) {
  289. return 102;
  290. }else{
  291. return 102;
  292. }
  293. }
  294. @end
  295. @interface WorkFlowCollectHeaderView : UICollectionReusableView
  296. @end
  297. @implementation WorkFlowCollectHeaderView
  298. @end
  299. @interface WorkFlowCollectFooterView : UICollectionReusableView
  300. @property (strong,nonatomic) UIButton *okButton;
  301. @property (strong,nonatomic) UIButton *cancelButton;
  302. @end
  303. @implementation WorkFlowCollectFooterView
  304. - (instancetype)initWithFrame:(CGRect)frame{
  305. self = [super initWithFrame: frame];
  306. if (self) {
  307. }
  308. return self;
  309. }
  310. @end
  311. //@interface WorkFlowCollectViewCell: UICollectionViewCell
  312. //@property (strong,nonatomic) UILabel *namelabel;
  313. //@end
  314. @implementation WorkFlowCollectViewCell
  315. - (instancetype)init{
  316. self = [super init];
  317. if (self) {
  318. }
  319. return self;
  320. }
  321. - (instancetype)initWithFrame:(CGRect)frame{
  322. self = [super initWithFrame:frame];
  323. if (self) {
  324. self.layer.cornerRadius = 5;
  325. self.layer.masksToBounds = YES;
  326. [self.contentView addSubview:self.namelabel];
  327. // self.namelabel.center = self.contentView.center;
  328. self.namelabel.frame = self.contentView.frame;
  329. }
  330. return self;
  331. }
  332. - (UILabel *)namelabel{
  333. if (!_namelabel) {
  334. _namelabel = [UILabel new];
  335. _namelabel.text = @"请家";
  336. _namelabel.font = [UIFont systemFontOfSize:13];
  337. _namelabel.textAlignment = NSTextAlignmentCenter;
  338. }
  339. return _namelabel;
  340. }
  341. - (void)selectNamel:(BOOL)isSelect
  342. {
  343. self.isSelected = isSelect;
  344. if (isSelect) {
  345. self.namelabel.textColor =textSeleColor_work;
  346. self.namelabel.backgroundColor = backSeleColor_work;
  347. }else{
  348. self.namelabel.textColor = textColor_work;
  349. self.namelabel.backgroundColor = backColor_work;
  350. }
  351. }
  352. @end