// // NY_SelectCView.m // 中安生态商城 // // Created by LanSha on 2017/7/25. // Copyright © 2017年 王鑫年. All rights reserved. // #import "SelectView.h" #import "UIButton+ImageTitleStyle.h" #import "NY_SelectCollectionViewCell.h" #import "Masonry.h" #import "SelectFiltrateView.h" #import #import "OnlyTicketView.h" #ifndef RGBA #define RGBA(r,g,b,a) \ [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a] #endif // 屏幕尺寸 #define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height // 主色调 #define MAINCOLOR RGBA(0, 142, 236, 1) static NSString *const NY_SelectCollectionViewCellID = @"NY_SelectCollectionViewCell"; static char *const btnKey = "btnKey"; static CGFloat filrateHeight = 188; @interface SelectView() { BOOL show; } @property (nonatomic, strong) SelectFiltrateView *filtrateView; @property (nonatomic, strong) NSArray *filtrateArr; @end @implementation SelectView - (instancetype)initWithFrame:(CGRect)frame withArr:(NSArray *)arr{ self = [super initWithFrame:frame]; if (self) { self.filtrateArr = arr; [self initUI]; } return self; } - (void)initUI{ // [self createCollectionView]; [self creatFiltrateView]; self.backgroundColor = [UIColor whiteColor]; UIView *mainView = [UIView new]; mainView.backgroundColor = RGBA(255, 255, 255, 1); [self addSubview:mainView]; [mainView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(40); make.left.right.top.equalTo(self); }]; UIView *topLine = [UIView new]; topLine.backgroundColor = RGBA(222, 222, 222, 1); [self addSubview:topLine]; [topLine mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(0.5); make.left.right.equalTo(mainView); make.top.mas_equalTo(mainView.mas_bottom); }]; NSArray *titleArr = @[@"综合",@"销量",@"价格",@"筛选"]; for (int i = 0; i < 4; i++) { UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:titleArr[i] forState:UIControlStateNormal ]; button.titleLabel.font = [UIFont systemFontOfSize:13]; [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor homeRedColor] forState:UIControlStateSelected]; [mainView addSubview:button]; button.tag = 100+i; [button mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(mainView).offset(KSCREEN_WIDTH/4*i); make.top.bottom.equalTo(mainView); make.width.mas_equalTo(KSCREEN_WIDTH/4); }]; if (i == _defaultSelectIndex) { button.selected = YES; } if (i == 2) { [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagenormal"] forState:UIControlStateNormal]; [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2]; objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN); } if (i == 3) { [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/filtrate_n"] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/filtrate_s"] forState:UIControlStateSelected]; [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2]; } } OnlyTicketView *onlyView = [[OnlyTicketView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 40)]; onlyView.backgroundColor = [UIColor whiteColor]; [onlyView.switchBtn addTarget:self action:@selector(onlyButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:onlyView]; } //只显示优惠券 - (void)onlyButtonAction:(UIButton *)sender{ if (self.delegate && [self.delegate respondsToSelector:@selector(selectItme:onlyTicketClick:)]) { [self.delegate selectItme:self onlyTicketClick:sender]; } } -(void)setSelectItmeArr:(NSArray *)selectItmeArr{ _selectItmeArr = selectItmeArr; _defaultSelectIndex = 0; UIButton *button = [self viewWithTag:102]; [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagenormal"] forState:UIControlStateNormal]; objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN); button.selected = NO; for (int i = 0; i<4 ;i++) { UIButton *button = [self viewWithTag:i+100]; if (i == 0) { button.selected = YES; continue; } if (i == 2) { continue; } button.selected = NO; if (i == 3) { [button setTitle:@"全部" forState:UIControlStateNormal]; [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2]; } } } /** 弹出视图 */ - (void)creatFiltrateView { self.filtrateView = [[SelectFiltrateView alloc] initWithFrame:CGRectMake(0, -filrateHeight, self.width, filrateHeight) filtrateArr:self.filtrateArr]; self.filtrateView.delegate = self; self.filtrateView.hidden = YES; [self addSubview:self.filtrateView]; } #pragma mark ----------- SelectFiltrateViewDelegate ------ /** 筛选确定事件 */ - (void)makeSureActionWithFromValue:(NSString *)fromValue toValue:(NSString *)toValue { UIButton *btn = (UIButton *)[self viewWithTag:103]; if ((fromValue.integerValue > 0) && (toValue.integerValue > fromValue.integerValue) &&(toValue.integerValue > 0)) { [btn setTitle:[NSString stringWithFormat:@"%@-%@",fromValue,toValue] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; }else if (fromValue.integerValue == 0 && toValue.integerValue > 0){ [btn setTitle:[NSString stringWithFormat:@"%@以下",toValue] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; }else if (toValue.integerValue < 0 && fromValue.integerValue > 0){ [btn setTitle:[NSString stringWithFormat:@"%@以上",fromValue] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; }else { [btn setTitle:@"筛选" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/filtrate_n"] forState:UIControlStateNormal]; } if (self.delegate && [self.delegate respondsToSelector:@selector(filtrateViewMakeSureFromValue:toValue:)]) { [self.delegate filtrateViewMakeSureFromValue:fromValue toValue:toValue]; } [self toggleViewWith:nil]; } - (void)selectClick:(UIButton *)btn{ if (btn.tag != 103) {//没点击全部分类,则让其他按钮回复默认状态 for (int i = 0; i<3 ;i++) { UIButton *button = [self viewWithTag:i+100]; button.selected = NO; } btn.selected = YES; [self toggleViewWith:nil]; }else{//当点击全部分类按钮,则 [self toggleViewWith:btn]; } ButtonClickType type = ButtonClickTypeNormal; if (btn.tag == 102) { NSString *flag = objc_getAssociatedObject(btn, btnKey); if ([flag isEqualToString:@"1"]) { [btn setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiageup"] forState:UIControlStateNormal]; objc_setAssociatedObject(btn, btnKey, @"2", OBJC_ASSOCIATION_ASSIGN); type = ButtonClickTypeUp; }else if ([flag isEqualToString:@"2"]){ [btn setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagedown"] forState:UIControlStateNormal]; objc_setAssociatedObject(btn, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN); type = ButtonClickTypeDown; } }else{ //点击全部不复位价格 if (btn.tag != 103) { UIButton *button = [self viewWithTag:102]; [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagenormal"] forState:UIControlStateNormal]; objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN); type = ButtonClickTypeNormal; } } if (btn.tag != 103) { if ([self.delegate respondsToSelector:@selector(selectTopButton:withIndex:withButtonType:)]) { [self.delegate selectTopButton:self withIndex:btn.tag-100 withButtonType:type]; } } } - (void)toggleViewWith:(UIButton *)btn{ if (!btn) { btn = [self viewWithTag:103]; if (show) { show = NO; }else{ return; } }else{ show = !show; } if (_defaultSelectItmeIndex != 0) { btn.selected = YES; }else{ btn.selected = NO; } if (show) { self.frame = CGRectMake(0, self.frame.origin.y, KSCREEN_WIDTH, SCREEN_HEIGHT); }else{ self.frame = CGRectMake(0, self.frame.origin.y, KSCREEN_WIDTH, 80); } float frameY = show?80:-self.filtrateView.height; self.filtrateView.hidden = !show; [UIView animateWithDuration:0.5 animations:^{ self.filtrateView.frame = CGRectMake(0, frameY, KSCREEN_WIDTH, filrateHeight); self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:show?0.4:0.0]; }]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self toggleViewWith:nil]; } @end