// // KBCategorySelectView.m // YouHuiProject // // Created by 小花 on 2018/10/9. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBCategorySelectView.h" #define kWidth [UIScreen mainScreen].bounds.size.width #define kHeight [UIScreen mainScreen].bounds.size.height-200 #define RGBColor(r,g,b,a) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]) @interface KBCategorySelectView () { NSInteger _selectedIndex; } @property (nonatomic, strong) UIView *alphaView; @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) NSArray *dataArr; @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation KBCategorySelectView - (instancetype)initWithmodels:(NSArray *)models delegate:(id)delegate selectedIndex:(NSInteger)index superVc:(id)superVc{ self = [super init]; if (self) { _selectedIndex = index; self.dataArr = [NSArray arrayWithArray:models]; if (delegate) { self.delegate = delegate; } [self initUIWithSuperVc:superVc]; } return self; } - (void)initUIWithSuperVc:(KBBaseViewController *)superVc { self.userInteractionEnabled = YES; self.backgroundColor = [UIColor clearColor]; self.frame = CGRectMake(0, superVc.navigationBar.bottom, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-NavBarHeight); _alphaView = [[UIView alloc] initWithFrame:self.bounds]; _alphaView.backgroundColor = [UIColor clearColor]; _alphaView.alpha = 0.0; UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:_alphaView.bounds]; [_alphaView addSubview:toolBar]; [self addSubview:_alphaView]; [self sendSubviewToBack:_alphaView]; [_alphaView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight]; [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight]; self.autoresizesSubviews = YES ; _alphaView.autoresizesSubviews = YES ; //取消 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)]; [_alphaView addGestureRecognizer:tapGesture]; NSInteger row = self.dataArr.count%4 == 0 ? self.dataArr.count/4:self.dataArr.count/4+1; row = row > 5? 5:row; CGFloat bgHeight = row * Fitsize(40) + 40; _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, -(NavBarHeight+kHeight), kWidth, bgHeight)]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 0.5)]; line.backgroundColor = [UIColor yhGrayColor]; [_bgView addSubview:line]; UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]; title.textColor = [UIColor YHColorWithHex:0x666666]; title.font = [UIFont systemFontOfSize:14]; title.text = @"全部分类"; title.centerX = _bgView.centerX; title.textAlignment = NSTextAlignmentCenter; [_bgView addSubview:title]; UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width-40, 0, 40, 40)]; [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal]; [cancelBtn addTarget:self action:@selector(tappedCancel) forControlEvents:UIControlEventTouchUpInside]; [_bgView addSubview:cancelBtn]; _bgView.backgroundColor = [UIColor whiteColor]; [self addSubview:_bgView]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH)/4, Fitsize(40)); flowLayout.minimumLineSpacing = 0; flowLayout.minimumInteritemSpacing = 0; flowLayout.headerReferenceSize = CGSizeMake(0, 0); self.collectionView = [[UICollectionView alloc]initWithFrame:_bgView.bounds collectionViewLayout:flowLayout]; [self.collectionView registerClass:[CategoryPopTypeCell class] forCellWithReuseIdentifier:@"CategoryPopTypeCell"]; self.collectionView.backgroundColor = [UIColor whiteColor]; self.collectionView.scrollEnabled = NO; self.collectionView.showsVerticalScrollIndicator = NO; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.y = 40; self.collectionView.height -= 40; self.collectionView.allowsMultipleSelection = YES; [_bgView addSubview: self.collectionView]; [self.collectionView reloadData]; } - (void)showInView:(UIView *)view belowSubView:(UIView *)subView { [view insertSubview:self belowSubview:subView]; [UIView animateWithDuration:0.35 animations:^{ _alphaView.alpha = 1; _bgView.y = 0; } completion:^(BOOL finished) { if (self.dataArr && self.dataArr.count>0) { [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionCenteredVertically]; } }]; } - (void)tappedCancel { [UIView animateWithDuration:0.35 animations:^{ _alphaView.alpha = 0.0; _bgView.y = -(NavBarHeight+kHeight); } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } - (void)didMoveToSuperview { } #pragma mark ----------------- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataArr.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CategoryPopTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryPopTypeCell" forIndexPath:indexPath]; cell.model = self.dataArr[indexPath.row]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.delegate && [self.delegate respondsToSelector:@selector(CategoryPopViewDidSelectedIndex:)]) { [self.delegate CategoryPopViewDidSelectedIndex:indexPath.row]; } [self tappedCancel]; } @end #pragma mark ===================== #pragma mark ------------------ @interface CategoryPopTypeCell() @end @implementation CategoryPopTypeCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self makeUI]; } return self; } - (void)setSelected:(BOOL)selected { if (selected) { self.label.textColor = [UIColor homeRedColor]; }else { self.label.textColor = [UIColor YHColorWithHex:0x444444]; } } - (void)setModel:(KBCategoryChannelModel *)model { self.label.text = model.name; } - (void)makeUI { [self addSubview:self.label]; [self.label mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.left.right.mas_equalTo(0); make.bottom.mas_equalTo(0); }]; } - (UILabel *)label { if (!_label) { _label = [[UILabel alloc] init]; _label.textAlignment = NSTextAlignmentCenter; _label.font = [UIFont systemFontOfSize:Fitsize(10)]; _label.textColor = [UIColor YHColorWithHex:0x444444]; } return _label; } @end