// // GroupListView.m // smartRhino // // Created by niuzhen on 2019/12/12. // Copyright © 2019 tederen. All rights reserved. // #import "GroupListView.h" #import "MenuListCell.h" #import "TopicGroupManageModel.h" @interface GroupListView() @property (nonatomic, strong)NSMutableArray * listArray; @property (nonatomic, strong)UIView * topView; @property (nonatomic, assign)CGFloat height; @property (nonatomic, strong)UIView * bgView; @property (nonatomic, strong)UICollectionView * collectionView; @end @implementation GroupListView + (instancetype)GroupListViewaddArray:(NSArray *)array showToView:(UIView *)view { return [[GroupListView alloc] initWithArray:array view:view]; } - (instancetype)initWithArray:(NSArray *)array view:(UIView *)view { if (self = [super init]) { self.listArray = [NSMutableArray arrayWithArray:array]; self.topView = view; [self setCusView:NO]; } return self; } - (UIView *)bgView { if (!_bgView) { _bgView = [UIView new]; _bgView.backgroundColor = [UIColor blackColor]; _bgView.alpha = 0.65f; } return _bgView; } - (void)setViewHeight:(CGFloat)height { [self setFrame:CGRectMake(0, height, SCREEN_WIDTH, SCREEN_HEIGHT - height)]; } - (void)setCusView:(BOOL)isHeight { [self setFrame: CGRectMake(0, CGRectGetMaxY(self.topView.frame) , SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.topView.frame))]; [self addSubview:self.bgView]; [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self); }]; WS(weakSelf); UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) { [weakSelf dismiss]; }]; [self.bgView addGestureRecognizer:tap]; [self addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.mas_equalTo(self); make.height.mas_equalTo(SCREEN_HEIGHT * 0.6); }]; } - (NSMutableArray *)listArray { if (!_listArray) { _listArray = [NSMutableArray array]; } return _listArray; } - (UICollectionView *)collectionView { if (!_collectionView) { _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]]; [_collectionView registerNib:[UINib nibWithNibName:@"MenuListCell" bundle:nil] forCellWithReuseIdentifier:@"MenuListCell"]; _collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15); _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"]; [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"]; } return _collectionView; } - (void)show { [[UIApplication sharedApplication].keyWindow addSubview:self]; [UIView animateWithDuration:0.4 animations:^{ } completion:^(BOOL finished) {}]; } - (void)dismiss { WS(weakSelf); [[UIApplication sharedApplication].keyWindow addSubview:self]; [UIView animateWithDuration:0.4 animations:^{ } completion:^(BOOL finished) { if (weakSelf.dismissBlock) { weakSelf.dismissBlock(); } [weakSelf removeFromSuperview]; }]; } #pragma mark - UICollectionViewDelegate - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { MenuListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuListCell" forIndexPath:indexPath]; TopicGroupManageModel * model = [self.listArray objectAtIndex:indexPath.item]; cell.titleL.text = model.Name; cell.titleL.backgroundColor = UIColorHex(#F5F6F8); if (model.isBtn) { cell.titleL.layer.borderColor = UIColorHex(#3979D3).CGColor; cell.titleL.layer.borderWidth = 0.5f; cell.titleL.backgroundColor = UIColorHex(#FFFFFF); cell.titleL.textColor = UIColorHex(#3979D3); }else{ cell.titleL.textColor = model.isSelect ? UIColorHex(#3979D3):UIColorHex(#0A0A0A); cell.titleL.layer.borderColor = model.isSelect ? UIColorHex(#3979D3).CGColor : [UIColor clearColor].CGColor; cell.titleL.layer.borderWidth = model.isSelect ? 0.5f : 0.f; } return cell; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.listArray.count; } -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ TopicGroupManageModel * model = [self.listArray objectAtIndex:indexPath.item]; UILabel * label = [UILabel new]; label.font = Kfont(13); label.text = model.Name; CGFloat W = [label sizeThatFits:CGSizeMake(MAXFLOAT, 36)].width + 20; return CGSizeMake(W, 36); } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ TopicGroupManageModel * model = [self.listArray objectAtIndex:indexPath.item]; for (TopicGroupManageModel * subModel in self.listArray) { subModel.isSelect = NO; } model.isSelect = !model.isSelect; if (self.SelectBtnblock) { self.SelectBtnblock(@[model]); } [collectionView reloadData]; [self dismiss]; } //footer的size - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(0, 0); } //header的size - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(SCREEN_WIDTH, 35); } //设置每个item的UIEdgeInsets - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 0, 0, 0); } //设置每个item水平间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 9.f; } //设置每个item垂直间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 18.f; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; UILabel *lablel = [[UILabel alloc] init]; lablel.textColor = UIColorHex(0xBBBBBB); [headerView addSubview:lablel]; lablel.text = @"选择显示"; lablel.font = [UIFont systemFontOfSize:11.f]; lablel.textAlignment = NSTextAlignmentLeft; [headerView addSubview:lablel]; [lablel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(headerView); make.centerY.mas_equalTo(headerView); }]; return headerView; }else{ UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath]; return footerView; } } @end