口袋优选

KBCategorySelectView.m 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // KBCategorySelectView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/10/9.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBCategorySelectView.h"
  9. #define kWidth [UIScreen mainScreen].bounds.size.width
  10. #define kHeight [UIScreen mainScreen].bounds.size.height-200
  11. #define RGBColor(r,g,b,a) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a])
  12. @interface KBCategorySelectView ()<UICollectionViewDelegate,UICollectionViewDataSource> {
  13. NSInteger _selectedIndex;
  14. }
  15. @property (nonatomic, strong) UIView *alphaView;
  16. @property (nonatomic, strong) UIView *bgView;
  17. @property (nonatomic, strong) NSArray *dataArr;
  18. @property (nonatomic, strong) UICollectionView *collectionView;
  19. @end
  20. @implementation KBCategorySelectView
  21. - (instancetype)initWithmodels:(NSArray *)models delegate:(id<CategoryPopViewDelegate>)delegate selectedIndex:(NSInteger)index superVc:(id)superVc{
  22. self = [super init];
  23. if (self) {
  24. _selectedIndex = index;
  25. self.dataArr = [NSArray arrayWithArray:models];
  26. if (delegate) {
  27. self.delegate = delegate;
  28. }
  29. [self initUIWithSuperVc:superVc];
  30. }
  31. return self;
  32. }
  33. - (void)initUIWithSuperVc:(KBBaseViewController *)superVc {
  34. self.userInteractionEnabled = YES;
  35. self.backgroundColor = [UIColor clearColor];
  36. self.frame = CGRectMake(0, superVc.navigationBar.bottom, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-NavBarHeight);
  37. _alphaView = [[UIView alloc] initWithFrame:self.bounds];
  38. _alphaView.backgroundColor = [UIColor clearColor];
  39. _alphaView.alpha = 0.0;
  40. UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:_alphaView.bounds];
  41. [_alphaView addSubview:toolBar];
  42. [self addSubview:_alphaView];
  43. [self sendSubviewToBack:_alphaView];
  44. [_alphaView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight];
  45. [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight];
  46. self.autoresizesSubviews = YES ;
  47. _alphaView.autoresizesSubviews = YES ;
  48. //取消
  49. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
  50. [_alphaView addGestureRecognizer:tapGesture];
  51. NSInteger row = self.dataArr.count%4 == 0 ? self.dataArr.count/4:self.dataArr.count/4+1;
  52. row = row > 5? 5:row;
  53. CGFloat bgHeight = row * Fitsize(40) + 40;
  54. _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, -(NavBarHeight+kHeight), kWidth, bgHeight)];
  55. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 0.5)];
  56. line.backgroundColor = [UIColor yhGrayColor];
  57. [_bgView addSubview:line];
  58. UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
  59. title.textColor = [UIColor YHColorWithHex:0x666666];
  60. title.font = [UIFont systemFontOfSize:14];
  61. title.text = @"全部分类";
  62. title.centerX = _bgView.centerX;
  63. title.textAlignment = NSTextAlignmentCenter;
  64. [_bgView addSubview:title];
  65. UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width-40, 0, 40, 40)];
  66. [cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  67. [cancelBtn addTarget:self action:@selector(tappedCancel) forControlEvents:UIControlEventTouchUpInside];
  68. [_bgView addSubview:cancelBtn];
  69. _bgView.backgroundColor = [UIColor whiteColor];
  70. [self addSubview:_bgView];
  71. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  72. flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH)/4, Fitsize(40));
  73. flowLayout.minimumLineSpacing = 0;
  74. flowLayout.minimumInteritemSpacing = 0;
  75. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  76. self.collectionView = [[UICollectionView alloc]initWithFrame:_bgView.bounds collectionViewLayout:flowLayout];
  77. [self.collectionView registerClass:[CategoryPopTypeCell class] forCellWithReuseIdentifier:@"CategoryPopTypeCell"];
  78. self.collectionView.backgroundColor = [UIColor whiteColor];
  79. self.collectionView.scrollEnabled = NO;
  80. self.collectionView.showsVerticalScrollIndicator = NO;
  81. self.collectionView.delegate = self;
  82. self.collectionView.dataSource = self;
  83. self.collectionView.y = 40;
  84. self.collectionView.height -= 40;
  85. self.collectionView.allowsMultipleSelection = YES;
  86. [_bgView addSubview: self.collectionView];
  87. [self.collectionView reloadData];
  88. }
  89. - (void)showInView:(UIView *)view belowSubView:(UIView *)subView {
  90. [view insertSubview:self belowSubview:subView];
  91. [UIView animateWithDuration:0.35 animations:^{
  92. _alphaView.alpha = 1;
  93. _bgView.y = 0;
  94. } completion:^(BOOL finished) {
  95. if (self.dataArr && self.dataArr.count>0) {
  96. [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  97. }
  98. }];
  99. }
  100. - (void)tappedCancel {
  101. [UIView animateWithDuration:0.35 animations:^{
  102. _alphaView.alpha = 0.0;
  103. _bgView.y = -(NavBarHeight+kHeight);
  104. } completion:^(BOOL finished) {
  105. [self removeFromSuperview];
  106. }];
  107. }
  108. - (void)didMoveToSuperview {
  109. }
  110. #pragma mark -----------------
  111. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  112. {
  113. return self.dataArr.count;
  114. }
  115. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. CategoryPopTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryPopTypeCell" forIndexPath:indexPath];
  118. cell.model = self.dataArr[indexPath.row];
  119. return cell;
  120. }
  121. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  122. if (self.delegate && [self.delegate respondsToSelector:@selector(CategoryPopViewDidSelectedIndex:)]) {
  123. [self.delegate CategoryPopViewDidSelectedIndex:indexPath.row];
  124. }
  125. [self tappedCancel];
  126. }
  127. @end
  128. #pragma mark =====================
  129. #pragma mark ------------------
  130. @interface CategoryPopTypeCell()
  131. @end
  132. @implementation CategoryPopTypeCell
  133. - (instancetype)initWithFrame:(CGRect)frame {
  134. self = [super initWithFrame:frame];
  135. if (self) {
  136. [self makeUI];
  137. }
  138. return self;
  139. }
  140. - (void)setSelected:(BOOL)selected {
  141. if (selected) {
  142. self.label.textColor = [UIColor homeRedColor];
  143. }else {
  144. self.label.textColor = [UIColor YHColorWithHex:0x444444];
  145. }
  146. }
  147. - (void)setModel:(KBCategoryChannelModel *)model {
  148. self.label.text = model.name;
  149. }
  150. - (void)makeUI {
  151. [self addSubview:self.label];
  152. [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
  153. make.top.mas_equalTo(0);
  154. make.left.right.mas_equalTo(0);
  155. make.bottom.mas_equalTo(0);
  156. }];
  157. }
  158. - (UILabel *)label {
  159. if (!_label) {
  160. _label = [[UILabel alloc] init];
  161. _label.textAlignment = NSTextAlignmentCenter;
  162. _label.font = [UIFont systemFontOfSize:Fitsize(10)];
  163. _label.textColor = [UIColor YHColorWithHex:0x444444];
  164. }
  165. return _label;
  166. }
  167. @end