// // SQFiltrateView.m // FiltrateView // // Created by quanminqianbao on 2017/12/7. // Copyright © 2017年 yangshuquan. All rights reserved. // #import "SQFiltrateView.h" #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width) #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height) @implementation SQFiltrateItem - (NSMutableSet *)choseSet{ if (_choseSet) { return _choseSet; } _choseSet = [NSMutableSet set]; return _choseSet; } @end @interface SQFiltrateView () @property (nonatomic, strong) UIButton *bg_button; @property (nonatomic, assign) NSUInteger select_itemIndex; @end; @implementation SQFiltrateView - (instancetype)initWithFrame:(CGRect)frame filtrateItems:(NSArray *)filtrateItems{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor whiteColor]; self.filtrateItems = filtrateItems; [self setViews]; } return self; } - (void)setViews{ for (UIView *subView in self.subviews) { [subView removeFromSuperview]; } UIView *lineView = [[UIView alloc]init]; lineView.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.5]; [self addSubview:lineView]; [self bringSubviewToFront:lineView]; lineView.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *lineView_left = [NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0]; NSLayoutConstraint *lineView_bottom = [NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]; NSLayoutConstraint *lineView_height = [NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0.5]; NSLayoutConstraint *lineView_right = [NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0]; [self addConstraints:@[lineView_left,lineView_bottom,lineView_height,lineView_right]]; CGRect selfRect = self.frame; //罗云飞自改地方 原版在学习资料里面。。。。 UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, 7, 1, 30)]; line.backgroundColor = NewLineGrayColor; [self addSubview:line]; if (self.filtrateItems.count == 1) { line.hidden = YES; }else{ line.hidden = NO; } _bg_button = [UIButton buttonWithType:UIButtonTypeCustom]; _bg_button.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4]; _bg_button.hidden = YES; _bg_button.frame = CGRectMake(CGRectGetMinX(selfRect), CGRectGetMaxY(selfRect), CGRectGetWidth(selfRect), SCREEN_HEIGHT-CGRectGetHeight(selfRect)); [[UIApplication sharedApplication].keyWindow addSubview:_bg_button]; [_bg_button addTarget:self action:@selector(bg_button_Action:) forControlEvents:UIControlEventTouchUpInside]; NSMutableArray *buttons = @[].mutableCopy; __block UIView *lastView = nil; [self.filtrateItems enumerateObjectsUsingBlock:^(SQFiltrateItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { SQCustomButton *button = [[SQCustomButton alloc]initWithFrame:CGRectZero type:SQCustomButtonRightImageType imageSize:CGSizeMake(11, 10) midmargin:2]; button.imageView.image = [UIImage imageNamed:@"borrow_down"]; button.titleLabel.text = obj.title; button.titleLabel.textColor = [UIColor grayColor]; button.titleLabel.font = [UIFont systemFontOfSize:15]; button.translatesAutoresizingMaskIntoConstraints = NO; [self addSubview:button]; [buttons addObject:button]; obj.button = button; typeof(self) weakSelf = self; [button touchAction:^(SQCustomButton * _Nonnull button) { weakSelf.select_itemIndex = [buttons indexOfObject:button]; [weakSelf click:obj]; }]; [self setListView:obj]; NSLayoutConstraint *button_left; if (lastView) { button_left = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:lastView attribute:NSLayoutAttributeRight multiplier:1 constant:0]; }else{ button_left = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0]; } NSLayoutConstraint *button_top = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0]; NSLayoutConstraint *button_bottom = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]; NSLayoutConstraint *lineView_width = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:SCREEN_WIDTH/self.filtrateItems.count]; [self addConstraints:@[button_left,button_top,button_bottom,lineView_width]]; lastView = button; }]; } - (void)setListView:(SQFiltrateItem *)filtrateItem{ UIView *tempView = [[UIView alloc]init]; tempView.backgroundColor = [UIColor whiteColor]; tempView.clipsToBounds = YES; [[UIApplication sharedApplication].keyWindow addSubview:tempView]; filtrateItem.bg_View = tempView; tempView.hidden = YES; if (filtrateItem.listType == OptionListType_Tag) { NSInteger eachNumber = 3; NSInteger tagHeight = 44; // CGFloat width = (self.frame.size.width-15*2-10*(eachNumber-1))/eachNumber; CGFloat width = SCREEN_WIDTH/eachNumber; NSMutableArray *tempArray = @[].mutableCopy; for (NSInteger i=0; i