SQFiltrateView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // SQFiltrateView.m
  3. // FiltrateView
  4. //
  5. // Created by quanminqianbao on 2017/12/7.
  6. // Copyright © 2017年 yangshuquan. All rights reserved.
  7. //
  8. #import "SQFiltrateView.h"
  9. #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
  10. #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
  11. @implementation SQFiltrateItem
  12. - (NSMutableSet *)choseSet{
  13. if (_choseSet) {
  14. return _choseSet;
  15. }
  16. _choseSet = [NSMutableSet set];
  17. return _choseSet;
  18. }
  19. @end
  20. @interface SQFiltrateView ()
  21. @property (nonatomic, strong) UIButton *bg_button;
  22. @property (nonatomic, assign) NSUInteger select_itemIndex;
  23. @end;
  24. @implementation SQFiltrateView
  25. - (instancetype)initWithFrame:(CGRect)frame filtrateItems:(NSArray<SQFiltrateItem *> *)filtrateItems{
  26. if (self = [super initWithFrame:frame]) {
  27. self.backgroundColor = [UIColor whiteColor];
  28. self.filtrateItems = filtrateItems;
  29. [self setViews];
  30. }
  31. return self;
  32. }
  33. - (void)setViews{
  34. for (UIView *subView in self.subviews) {
  35. [subView removeFromSuperview];
  36. }
  37. UIView *lineView = [[UIView alloc]init];
  38. lineView.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.5];
  39. [self addSubview:lineView];
  40. [self bringSubviewToFront:lineView];
  41. lineView.translatesAutoresizingMaskIntoConstraints = NO;
  42. NSLayoutConstraint *lineView_left = [NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
  43. NSLayoutConstraint *lineView_bottom = [NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
  44. NSLayoutConstraint *lineView_height = [NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0.5];
  45. NSLayoutConstraint *lineView_right = [NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0];
  46. [self addConstraints:@[lineView_left,lineView_bottom,lineView_height,lineView_right]];
  47. CGRect selfRect = self.frame;
  48. //罗云飞自改地方 原版在学习资料里面。。。。
  49. UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, 7, 1, 30)];
  50. line.backgroundColor = NewLineGrayColor;
  51. [self addSubview:line];
  52. if (self.filtrateItems.count == 1) {
  53. line.hidden = YES;
  54. }else{
  55. line.hidden = NO;
  56. }
  57. _bg_button = [UIButton buttonWithType:UIButtonTypeCustom];
  58. _bg_button.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
  59. _bg_button.hidden = YES;
  60. _bg_button.frame = CGRectMake(CGRectGetMinX(selfRect), CGRectGetMaxY(selfRect), CGRectGetWidth(selfRect), SCREEN_HEIGHT-CGRectGetHeight(selfRect));
  61. [[UIApplication sharedApplication].keyWindow addSubview:_bg_button];
  62. [_bg_button addTarget:self action:@selector(bg_button_Action:) forControlEvents:UIControlEventTouchUpInside];
  63. NSMutableArray *buttons = @[].mutableCopy;
  64. __block UIView *lastView = nil;
  65. [self.filtrateItems enumerateObjectsUsingBlock:^(SQFiltrateItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  66. SQCustomButton *button = [[SQCustomButton alloc]initWithFrame:CGRectZero
  67. type:SQCustomButtonRightImageType
  68. imageSize:CGSizeMake(11, 10)
  69. midmargin:2];
  70. button.imageView.image = [UIImage imageNamed:@"borrow_down"];
  71. button.titleLabel.text = obj.title;
  72. button.titleLabel.textColor = [UIColor grayColor];
  73. button.titleLabel.font = [UIFont systemFontOfSize:15];
  74. button.translatesAutoresizingMaskIntoConstraints = NO;
  75. [self addSubview:button];
  76. [buttons addObject:button];
  77. obj.button = button;
  78. typeof(self) weakSelf = self;
  79. [button touchAction:^(SQCustomButton * _Nonnull button) {
  80. weakSelf.select_itemIndex = [buttons indexOfObject:button];
  81. [weakSelf click:obj];
  82. }];
  83. [self setListView:obj];
  84. NSLayoutConstraint *button_left;
  85. if (lastView) {
  86. button_left = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:lastView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
  87. }else{
  88. button_left = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
  89. }
  90. NSLayoutConstraint *button_top = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0];
  91. NSLayoutConstraint *button_bottom = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
  92. NSLayoutConstraint *lineView_width = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:SCREEN_WIDTH/self.filtrateItems.count];
  93. [self addConstraints:@[button_left,button_top,button_bottom,lineView_width]];
  94. lastView = button;
  95. }];
  96. }
  97. - (void)setListView:(SQFiltrateItem *)filtrateItem{
  98. UIView *tempView = [[UIView alloc]init];
  99. tempView.backgroundColor = [UIColor whiteColor];
  100. tempView.clipsToBounds = YES;
  101. [[UIApplication sharedApplication].keyWindow addSubview:tempView];
  102. filtrateItem.bg_View = tempView;
  103. tempView.hidden = YES;
  104. if (filtrateItem.listType == OptionListType_Tag) {
  105. NSInteger eachNumber = 3;
  106. NSInteger tagHeight = 44;
  107. // CGFloat width = (self.frame.size.width-15*2-10*(eachNumber-1))/eachNumber;
  108. CGFloat width = SCREEN_WIDTH/eachNumber;
  109. NSMutableArray *tempArray = @[].mutableCopy;
  110. for (NSInteger i=0; i<filtrateItem.optionData.count; i++) {
  111. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  112. [button setTitle:filtrateItem.optionData[i] forState:0];
  113. [button setTitleColor:[UIColor colorWithString:@"#9A9A9A"] forState:0];
  114. // button.layer.cornerRadius = 2.f;
  115. button.titleLabel.font = [UIFont systemFontOfSize:13];
  116. button.layer.borderWidth = 0.5;
  117. button.layer.borderColor = NewGroupTableViewBackgroundColor.CGColor;
  118. button.tag = i;
  119. button.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
  120. NSInteger a = i/eachNumber;
  121. NSInteger b = i%eachNumber;
  122. // button.frame = (CGRect){15+(width+10)*b,15+(tagHeight+10)*a,width,tagHeight};
  123. button.frame = (CGRect){width*b,0+(tagHeight+0)*a,width,tagHeight};
  124. [tempView addSubview:button];
  125. if (i==filtrateItem.optionData.count-1) {
  126. tempView.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMaxY(self.frame), CGRectGetWidth(self.frame),0);
  127. filtrateItem.bg_ViewHeight = CGRectGetMaxY(button.frame);
  128. }
  129. [tempArray addObject:button];
  130. [button addTarget:self action:@selector(select_button_action:) forControlEvents:UIControlEventTouchUpInside];
  131. }
  132. filtrateItem.listCellViews = tempArray;
  133. }
  134. if (filtrateItem.listType == OptionListType_Cell) {
  135. CGFloat cellHeight = 45.f;
  136. NSMutableArray *tempArray = @[].mutableCopy;
  137. for (NSInteger i=0; i<filtrateItem.optionData.count; i++){
  138. UIView *labeltempView = [[UIView alloc]initWithFrame:CGRectMake(0, (cellHeight+0.5)*i, SCREEN_WIDTH, cellHeight)];
  139. [tempView addSubview:labeltempView];
  140. labeltempView.tag = i;
  141. UILabel *label = [[UILabel alloc]init];
  142. label.textColor = [UIColor grayColor];
  143. label.font = [UIFont systemFontOfSize:14];
  144. label.text = filtrateItem.optionData[i];
  145. [labeltempView addSubview:label];
  146. [tempArray addObject:label];
  147. label.frame = CGRectMake(15, 0, SCREEN_WIDTH-15, cellHeight);
  148. if (i<filtrateItem.optionData.count-1) {
  149. UIView *lineView = [UIView new];
  150. lineView.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.5];
  151. [labeltempView addSubview:lineView];
  152. lineView.frame = CGRectMake(15, cellHeight-0.5, SCREEN_WIDTH-15, 0.5);
  153. }
  154. if (i==filtrateItem.optionData.count-1) {
  155. tempView.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMaxY(self.frame), CGRectGetWidth(self.frame),0);
  156. filtrateItem.bg_ViewHeight = CGRectGetMaxY(labeltempView.frame)+0.5;
  157. }
  158. labeltempView.tag = i;
  159. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTap:)];
  160. [labeltempView addGestureRecognizer:tap];
  161. }
  162. filtrateItem.listCellViews = tempArray;
  163. }
  164. }
  165. - (void)hideAllItemView{
  166. for (SQFiltrateItem *item in self.filtrateItems) {
  167. item.isShow = YES;
  168. }
  169. [self click:self.filtrateItems[0]];
  170. }
  171. - (void)click:(SQFiltrateItem *)filtrateItem{
  172. for (SQFiltrateItem *item in self.filtrateItems) {
  173. if ([item.title isEqualToString:filtrateItem.title]) {
  174. item.isShow = !item.isShow;
  175. if (item.isShow) {
  176. item.bg_View.hidden = NO;
  177. [UIView animateWithDuration:0.2 animations:^{
  178. CGRect fram = item.bg_View.frame;
  179. fram.size.height = item.bg_ViewHeight;
  180. item.bg_View.frame = fram;
  181. _bg_button.hidden = NO;
  182. item.button.imageView.image = [UIImage imageNamed:@"borrow_up"];
  183. item.button.titleLabel.textColor = [UIColor colorWithString:@"#0368A9"];
  184. } completion:^(BOOL finished) {
  185. }];
  186. }else{
  187. item.bg_View.hidden = YES;
  188. [UIView animateWithDuration:0.2 animations:^{
  189. CGRect fram = item.bg_View.frame;
  190. fram.size.height = 0;
  191. item.bg_View.frame = fram;
  192. _bg_button.hidden = YES;
  193. item.button.imageView.image = [UIImage imageNamed:@"borrow_down"];
  194. item.button.titleLabel.textColor = [UIColor colorWithString:@"#9A9A9A"];
  195. } completion:^(BOOL finished) {
  196. }];
  197. }
  198. }else{
  199. item.isShow = NO;
  200. item.bg_View.hidden = YES;
  201. CGRect fram = item.bg_View.frame;
  202. fram.size.height = 0;
  203. item.bg_View.frame = fram;
  204. item.button.imageView.image = [UIImage imageNamed:@"borrow_down"];
  205. item.button.titleLabel.textColor = [UIColor colorWithString:@"#9A9A9A"];
  206. }
  207. }
  208. }
  209. - (void)refreshListTag:(NSInteger)tag filtrateItem:(SQFiltrateItem *)filtrateItem{
  210. if ([filtrateItem.choseSet containsObject:@(tag)]) {
  211. [filtrateItem.choseSet removeObject:@(tag)];
  212. }else{
  213. if (filtrateItem.numberType == OptionNumberType_Single) {
  214. //单选
  215. [filtrateItem.choseSet removeAllObjects];
  216. [filtrateItem.choseSet addObject:@(tag)];
  217. }else{
  218. //多选
  219. [filtrateItem.choseSet addObject:@(tag)];
  220. }
  221. }
  222. for (NSInteger i=0; i<filtrateItem.listCellViews.count; i++) {
  223. if ([filtrateItem.listCellViews[i] isKindOfClass:[UIButton class]]) {
  224. UIButton *button = (UIButton *)filtrateItem.listCellViews[i];
  225. [button setTitleColor:[filtrateItem.choseSet containsObject:@(i)]?[UIColor colorWithString:@"#0368A9"]:[UIColor colorWithString:@"#9A9A9A"] forState:0];
  226. // button.layer.borderColor = ([filtrateItem.choseSet containsObject:@(i)]?[UIColor colorWithString:@"#0368A9"]:[UIColor colorWithString:@"#9A9A9A"]).CGColor;
  227. filtrateItem.button.titleLabel.text = filtrateItem.optionData[tag];
  228. }
  229. if ([filtrateItem.listCellViews[i] isKindOfClass:[UILabel class]]) {
  230. UILabel *label = (UILabel *)filtrateItem.listCellViews[i];
  231. [label setTextColor:([filtrateItem.choseSet containsObject:@(i)]?[UIColor colorWithString:@"#0368A9"]:[UIColor colorWithString:@"#9A9A9A"])];
  232. filtrateItem.button.titleLabel.text = filtrateItem.optionData[tag];
  233. }
  234. }
  235. if (!filtrateItem.choseSet.count) {
  236. filtrateItem.button.titleLabel.text = filtrateItem.title;
  237. }
  238. if (self.touchBlock) {
  239. self.touchBlock(self, filtrateItem);
  240. }
  241. if (filtrateItem.numberType == OptionNumberType_Single) {
  242. [self hideAllItemView];
  243. }
  244. }
  245. - (void)setFiltrateItems:(NSArray *)filtrateItems{
  246. _filtrateItems = filtrateItems;
  247. [self setViews];
  248. }
  249. - (void)touchBlock:(void (^)(SQFiltrateView *, SQFiltrateItem *))block{
  250. self.touchBlock = block;
  251. }
  252. - (void)bg_button_Action:(UIButton *)button{
  253. [self hideAllItemView];
  254. }
  255. - (void)select_button_action:(UIButton *)button{
  256. SQFiltrateItem *item = self.filtrateItems[self.select_itemIndex];
  257. [self refreshListTag:button.tag filtrateItem:item];
  258. }
  259. - (void)labelTap:(UITapGestureRecognizer *)tap{
  260. SQFiltrateItem *item = self.filtrateItems[self.select_itemIndex];
  261. [self refreshListTag:tap.view.tag filtrateItem:item];
  262. }
  263. @end