// // topicFilterControlelr.m // ThePaperHD // // Created by scar1900 on 15/4/24. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "topicFilterControlelr.h" #import "filterSelectBtn.h" @interface topicFilterControlelr () { CGFloat topPadding; CGFloat lineNum; CGFloat filterBackHeight; CGFloat backBtnHeight; } @property(nonatomic, strong)UIButton *maskBtn; @property(nonatomic, strong)UIView *searchView; @property(nonatomic, strong)UIView *filterBackView; @property(nonatomic, strong)UIView *btnBackView; @property(nonatomic, strong)NSMutableArray *filterBtnList; @property(nonatomic, strong)UIView *sortBtnBack; @property(nonatomic, strong)UIButton *hotSortBtn; @property(nonatomic, strong)UIButton *timeSortBtn; @end @implementation topicFilterControlelr @synthesize cataList = _cataList; @synthesize category = _category,isHot = _isHot; @synthesize filterBtnList; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor clearColor]; if (isIOS7) { topPadding = 64; }else topPadding = 44; CGFloat num = self.cataList.count/7; CGFloat remind = self.cataList.count%7; if (remind == 0) { lineNum = (int)num; }else { lineNum = (int)num + 1; } [self.view addSubview:self.maskBtn]; [self.view addSubview:self.filterBackView]; [self.filterBackView addSubview:self.btnBackView]; [self.filterBackView addSubview:self.searchView]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)setCataList:(NSArray *)list { _cataList = list; } - (void)setCategory:(categoryBO *)cate { _category = cate; } - (void)setIsHot:(NSString *)hot { _isHot = hot; } - (UIButton*)maskBtn { if (!_maskBtn) { _maskBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _maskBtn.backgroundColor = [UIColor clearColor]; _maskBtn.frame = self.view.bounds; [_maskBtn addTarget:self action:@selector(clickToDismiss) forControlEvents:UIControlEventTouchUpInside]; } return _maskBtn; } -(UIView *)searchView{//【需求】搜索:增加话题的搜索功能(bug:4314) if (!_searchView) { _searchView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)/2 - 375/2, topPadding-20, 375, 30)]; _searchView.backgroundColor = [UIColor clearColor]; UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; icon.image= Image(@"topic/topicFilterSearch.png"); [_searchView addSubview:icon]; UITextField *searContent = [[UITextField alloc] initWithFrame:CGRectMake(25, 0, 310, 20)]; searContent.placeholder = @"话题搜索"; [_searchView addSubview:searContent]; UILabel *searchLabel = [[UILabel alloc] initWithFrame:CGRectMake(335, 0, 40, 30)]; searchLabel.text = @"搜索"; searchLabel.font = appFont(15, NO); searchLabel.textColor = [UIColor colorWithHexString:TextBlack]; searchLabel.backgroundColor = [UIColor clearColor]; [_searchView addSubview:searchLabel]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 29, 335, 1)]; line.backgroundColor = [UIColor whiteColor]; [_searchView addSubview:line]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.backgroundColor = [UIColor clearColor]; [btn addTarget:self action:@selector(searchSelector:) forControlEvents:UIControlEventTouchUpInside]; btn.frame = _searchView.bounds; [_searchView addSubview:btn]; } return _searchView; } - (UIView*)filterBackView { if (!_filterBackView) { filterBackHeight = 180; filterBackHeight = filterBackHeight+lineNum*50; _filterBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0-filterBackHeight+topPadding, CGRectGetWidth(self.view.bounds), filterBackHeight)]; _filterBackView.backgroundColor = [UIColor colorWithHexString:@"0xddd7cd"]; } return _filterBackView; } - (UIView*)btnBackView { if (!_btnBackView) { backBtnHeight = 80; backBtnHeight = backBtnHeight+lineNum*50; _btnBackView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.filterBackView.bounds)/2-768/2, 80, 768, backBtnHeight)]; _btnBackView.backgroundColor = [UIColor clearColor]; } return _btnBackView; } - (UIView*)sortBtnBack { if (!_sortBtnBack) { _sortBtnBack = [[UIView alloc]initWithFrame:CGRectMake(768/2-330/2, CGRectGetHeight(self.btnBackView.bounds)-31, 330, 30)]; _sortBtnBack.backgroundColor = [UIColor whiteColor]; _sortBtnBack.layer.cornerRadius = 4; self.hotSortBtn.frame = CGRectMake(0, 0, CGRectGetWidth(_sortBtnBack.bounds)/2, CGRectGetHeight(_sortBtnBack.bounds)); [_sortBtnBack addSubview:self.hotSortBtn]; self.timeSortBtn.frame = CGRectMake(CGRectGetMaxX(self.hotSortBtn.frame), 0, CGRectGetWidth(_sortBtnBack.bounds)/2, CGRectGetHeight(_sortBtnBack.bounds)); [_sortBtnBack addSubview:self.timeSortBtn]; UIView *sortLine = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.hotSortBtn.frame), 0, 1, CGRectGetHeight(_sortBtnBack.bounds))]; sortLine.backgroundColor = [UIColor colorWithHexString:@"0xe4eaed"]; [_sortBtnBack addSubview:sortLine]; } return _sortBtnBack; } - (UIButton*)hotSortBtn { if (!_hotSortBtn) { _hotSortBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _hotSortBtn.backgroundColor = [UIColor clearColor]; [_hotSortBtn setTitle:@"热门" forState:UIControlStateNormal]; [_hotSortBtn setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateNormal]; [_hotSortBtn setTitleColor:[UIColor colorWithHexString:@"0xc57130"] forState:UIControlStateSelected]; [_hotSortBtn setImage:Image(@"topic/hotFilter.png") forState:UIControlStateNormal]; [_hotSortBtn setImage:Image(@"topic/hotFilter_s.png") forState:UIControlStateSelected]; _hotSortBtn.titleLabel.font = appFont(13, NO); [_hotSortBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 60, 5, 90)]; [_hotSortBtn setTitleEdgeInsets:UIEdgeInsetsMake(8, 65, 8, 55)]; [_hotSortBtn addTarget:self action:@selector(clickHotSort:) forControlEvents:UIControlEventTouchUpInside]; } return _hotSortBtn; } - (UIButton*)timeSortBtn { if (!_timeSortBtn) { _timeSortBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _timeSortBtn.backgroundColor = [UIColor clearColor]; // [_timeSortBtn setTitle:@"最近" forState:UIControlStateNormal] [_timeSortBtn setTitle:@"最新" forState:UIControlStateNormal];//问吧的话题列表排序设置页面中的“最近”应该修改为“最新”(bug:4265) [_timeSortBtn setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateNormal]; [_timeSortBtn setTitleColor:[UIColor colorWithHexString:@"0xc57130"] forState:UIControlStateSelected]; [_timeSortBtn setImage:Image(@"topic/timeFilter.png") forState:UIControlStateNormal]; [_timeSortBtn setImage:Image(@"topic/timeFilter_s.png") forState:UIControlStateSelected]; _timeSortBtn.titleLabel.font = appFont(13, NO); [_timeSortBtn setImageEdgeInsets:UIEdgeInsetsMake(6, 114/2, 4, 176/2)]; [_timeSortBtn setTitleEdgeInsets:UIEdgeInsetsMake(8, 65, 8, 50)]; [_timeSortBtn addTarget:self action:@selector(clickTimeSort:) forControlEvents:UIControlEventTouchUpInside]; } return _timeSortBtn; } - (void)showFilter { self.filterBtnList = [NSMutableArray array]; __block CGFloat width = 768/7; __block CGFloat height = 50; [self.cataList enumerateObjectsUsingBlock:^(categoryBO* cate, NSUInteger idx, BOOL *stop) { CGFloat lineF = idx/7; int line = (int)lineF; CGFloat row = 0; if (line == 0) { row = idx; }else { row = idx - 7; } CGRect btnFrame = CGRectMake(row*width, line*height, width, height); filterSelectBtn *btn = [[filterSelectBtn alloc]initWithFrame:btnFrame category:cate]; btn.delegate = self; if (row == 0) { btn.isHaveLine = NO; } if (self.category) { if ([cate.category isEqualToString:self.category.category]) { [btn setBtnSelected:YES]; }else { [btn setBtnSelected:NO]; } } [self.filterBtnList addObject:btn]; [self.btnBackView addSubview:btn]; }]; [self.btnBackView addSubview:self.sortBtnBack]; if ([self.isHot intValue] == 1) { self.hotSortBtn.selected = YES; self.timeSortBtn.selected = NO; }else { self.hotSortBtn.selected = NO; self.timeSortBtn.selected = YES; } if (isIOS7) { [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; [UIView animateWithDuration:0.35 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:4.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ CGRect frame = self.filterBackView.frame; CGFloat top = 20; if (isNotIOS8) { top = 0; } frame.origin.y = topPadding-top; self.filterBackView.frame = frame; } completion:^(BOOL finished) { [[UIApplication sharedApplication] endIgnoringInteractionEvents]; }]; }else { [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; [UIView animateWithDuration:0.25 animations:^{ CGRect frame = self.filterBackView.frame; frame.origin.y = topPadding; self.filterBackView.frame = frame; } completion:^(BOOL finished) { [[UIApplication sharedApplication] endIgnoringInteractionEvents]; }]; } } - (void)dismissFilter:(void (^)(void))completion{ [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; [UIView animateWithDuration:0.25 animations:^{ CGRect frame = self.filterBackView.frame; CGFloat top = 20; if (isNotIOS8) { top = 0; } frame.origin.y = -230+topPadding-top; self.filterBackView.frame = frame; } completion:^(BOOL finished) { [[UIApplication sharedApplication] endIgnoringInteractionEvents]; if (completion) { completion(); } }]; } - (void)statusBarOrientationChange:(NSNotification *)notification { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; CGFloat filterWidth = 0; if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { filterWidth = 768; self.maskBtn.frame = CGRectMake(0, 0, 768, 1024); }else { filterWidth = 1024; self.maskBtn.frame = CGRectMake(0, 0, 1024, 768); } self.searchView.frame = CGRectMake(filterWidth/2 - 375/2, topPadding-20, 375, 30); self.filterBackView.frame = CGRectMake(0,topPadding-20 , filterWidth, filterBackHeight); self.btnBackView.frame = CGRectMake(CGRectGetWidth(self.filterBackView.bounds)/2-768/2, CGRectGetMaxY(self.searchView.frame), 768, backBtnHeight); } - (void)clickToDismiss { if ([self.delegate respondsToSelector:@selector(clickMastToDismiss)]) { [self.delegate clickMastToDismiss]; } } #pragma mark - filterselection delegate - (void)selectFilterBtn:(categoryBO *)cate { [self.filterBtnList enumerateObjectsUsingBlock:^(filterSelectBtn* obj, NSUInteger idx, BOOL *stop) { if (![obj.category.category isEqualToString:cate.category]) { [obj setBtnSelected:NO]; } }]; self.category = cate; [self clickToDismiss]; } - (void)clickHotSort:(UIButton*)btn { if (btn.selected) { return; } btn.selected = !btn.selected; self.timeSortBtn.selected = NO; self.isHot = @"1"; [self clickToDismiss]; } - (void)clickTimeSort:(UIButton*)btn { if (btn.selected) { return; } btn.selected = !btn.selected; self.hotSortBtn.selected = NO; self.isHot = @"0"; [self clickToDismiss]; } -(void) searchSelector:(UIButton *)btn{//【需求】搜索:增加话题的搜索功能(bug:4314) if ([self.delegate respondsToSelector:@selector(filterToVc)]) { [self.delegate filterToVc]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end