GroupListView.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // GroupListView.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2019/12/12.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "GroupListView.h"
  9. #import "MenuListCell.h"
  10. #import "TopicGroupManageModel.h"
  11. @interface GroupListView()<UICollectionViewDelegate,UICollectionViewDataSource>
  12. @property (nonatomic, strong)NSMutableArray * listArray;
  13. @property (nonatomic, strong)UIView * topView;
  14. @property (nonatomic, assign)CGFloat height;
  15. @property (nonatomic, strong)UIView * bgView;
  16. @property (nonatomic, strong)UICollectionView * collectionView;
  17. @end
  18. @implementation GroupListView
  19. + (instancetype)GroupListViewaddArray:(NSArray *)array showToView:(UIView *)view
  20. {
  21. return [[GroupListView alloc] initWithArray:array view:view];
  22. }
  23. - (instancetype)initWithArray:(NSArray *)array view:(UIView *)view
  24. {
  25. if (self = [super init]) {
  26. self.listArray = [NSMutableArray arrayWithArray:array];
  27. self.topView = view;
  28. [self setCusView:NO];
  29. }
  30. return self;
  31. }
  32. - (UIView *)bgView
  33. {
  34. if (!_bgView) {
  35. _bgView = [UIView new];
  36. _bgView.backgroundColor = [UIColor blackColor];
  37. _bgView.alpha = 0.65f;
  38. }
  39. return _bgView;
  40. }
  41. - (void)setViewHeight:(CGFloat)height
  42. {
  43. [self setFrame:CGRectMake(0, height, SCREEN_WIDTH, SCREEN_HEIGHT - height)];
  44. }
  45. - (void)setCusView:(BOOL)isHeight
  46. {
  47. [self setFrame: CGRectMake(0, CGRectGetMaxY(self.topView.frame) , SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.topView.frame))];
  48. [self addSubview:self.bgView];
  49. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.edges.mas_equalTo(self);
  51. }];
  52. WS(weakSelf);
  53. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) {
  54. [weakSelf dismiss];
  55. }];
  56. [self.bgView addGestureRecognizer:tap];
  57. [self addSubview:self.collectionView];
  58. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.left.top.right.mas_equalTo(self);
  60. make.height.mas_equalTo(SCREEN_HEIGHT * 0.6);
  61. }];
  62. }
  63. - (NSMutableArray *)listArray
  64. {
  65. if (!_listArray) {
  66. _listArray = [NSMutableArray array];
  67. }
  68. return _listArray;
  69. }
  70. - (UICollectionView *)collectionView
  71. {
  72. if (!_collectionView) {
  73. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
  74. [_collectionView registerNib:[UINib nibWithNibName:@"MenuListCell" bundle:nil] forCellWithReuseIdentifier:@"MenuListCell"];
  75. _collectionView.backgroundColor = [UIColor whiteColor];
  76. _collectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
  77. _collectionView.delegate = self;
  78. _collectionView.dataSource = self;
  79. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
  80. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
  81. }
  82. return _collectionView;
  83. }
  84. - (void)show
  85. {
  86. [[UIApplication sharedApplication].keyWindow addSubview:self];
  87. [UIView animateWithDuration:0.4 animations:^{
  88. } completion:^(BOOL finished) {}];
  89. }
  90. - (void)dismiss
  91. {
  92. WS(weakSelf);
  93. [[UIApplication sharedApplication].keyWindow addSubview:self];
  94. [UIView animateWithDuration:0.4 animations:^{
  95. } completion:^(BOOL finished) {
  96. if (weakSelf.dismissBlock) {
  97. weakSelf.dismissBlock();
  98. }
  99. [weakSelf removeFromSuperview];
  100. }];
  101. }
  102. #pragma mark - UICollectionViewDelegate
  103. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  104. MenuListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuListCell" forIndexPath:indexPath];
  105. TopicGroupManageModel * model = [self.listArray objectAtIndex:indexPath.item];
  106. cell.titleL.text = model.Name;
  107. cell.titleL.backgroundColor = UIColorHex(#F5F6F8);
  108. if (model.isBtn) {
  109. cell.titleL.layer.borderColor = UIColorHex(#3979D3).CGColor;
  110. cell.titleL.layer.borderWidth = 0.5f;
  111. cell.titleL.backgroundColor = UIColorHex(#FFFFFF);
  112. cell.titleL.textColor = UIColorHex(#3979D3);
  113. }else{
  114. cell.titleL.textColor = model.isSelect ? UIColorHex(#3979D3):UIColorHex(#0A0A0A);
  115. cell.titleL.layer.borderColor = model.isSelect ? UIColorHex(#3979D3).CGColor : [UIColor clearColor].CGColor;
  116. cell.titleL.layer.borderWidth = model.isSelect ? 0.5f : 0.f;
  117. }
  118. return cell;
  119. }
  120. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  121. return self.listArray.count;
  122. }
  123. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  124. TopicGroupManageModel * model = [self.listArray objectAtIndex:indexPath.item];
  125. UILabel * label = [UILabel new];
  126. label.font = Kfont(13);
  127. label.text = model.Name;
  128. CGFloat W = [label sizeThatFits:CGSizeMake(MAXFLOAT, 36)].width + 20;
  129. return CGSizeMake(W, 36);
  130. }
  131. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  132. TopicGroupManageModel * model = [self.listArray objectAtIndex:indexPath.item];
  133. for (TopicGroupManageModel * subModel in self.listArray) {
  134. subModel.isSelect = NO;
  135. }
  136. model.isSelect = !model.isSelect;
  137. if (self.SelectBtnblock) {
  138. self.SelectBtnblock(@[model]);
  139. }
  140. [collectionView reloadData];
  141. [self dismiss];
  142. }
  143. //footer的size
  144. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  145. {
  146. return CGSizeMake(0, 0);
  147. }
  148. //header的size
  149. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  150. {
  151. return CGSizeMake(SCREEN_WIDTH, 35);
  152. }
  153. //设置每个item的UIEdgeInsets
  154. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  155. {
  156. return UIEdgeInsetsMake(0, 0, 0, 0);
  157. }
  158. //设置每个item水平间距
  159. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  160. {
  161. return 9.f;
  162. }
  163. //设置每个item垂直间距
  164. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  165. {
  166. return 18.f;
  167. }
  168. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  169. if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  170. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
  171. UILabel *lablel = [[UILabel alloc] init];
  172. lablel.textColor = UIColorHex(0xBBBBBB);
  173. [headerView addSubview:lablel];
  174. lablel.text = @"选择显示";
  175. lablel.font = [UIFont systemFontOfSize:11.f];
  176. lablel.textAlignment = NSTextAlignmentLeft;
  177. [headerView addSubview:lablel];
  178. [lablel mas_makeConstraints:^(MASConstraintMaker *make) {
  179. make.left.mas_equalTo(headerView);
  180. make.centerY.mas_equalTo(headerView);
  181. }];
  182. return headerView;
  183. }else{
  184. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
  185. return footerView;
  186. }
  187. }
  188. @end