// // KXNewMainPageViewController.m // CAISHEN // // Created by xiaoxi on 2017/12/5. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXNewMainPageViewController.h" #import "KXOptionButtonView.h" #import "KXNewsTableViewCell.h" #import "KXNewsWebViewController.h" #import "KXBannerCollectionViewCell.h" #import "KXHouseCalculateViewController.h" #import "KXEqualRateToolViewController.h" #import "KXIndividualIncomeViewController.h" #import "KXInstallmentViewController.h" #import "KXCarLoancalViewController.h" #import "KXBaseLinkViewController.h" #import "KXNewKeySearchViewController.h" #import "KXCustomServiceViewController.h" @interface KXNewMainPageViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UITextField *searchTextField; @property (nonatomic, strong) UIImageView *bannerImageView; @property (nonatomic, strong) UIView *headerView; @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *tableViewDataSource; @property (nonatomic, strong) NSArray *collectionViewDataSource; @property (nonatomic, assign) NSInteger page; @property (nonatomic, assign) NSInteger index; @property (nonatomic, strong) KXOptionButtonView *optionView; @property (nonatomic, strong) NSMutableArray *titleArray; @property (nonatomic, strong) UIScrollView *scrollView; @end @implementation KXNewMainPageViewController { NSString *_bannerID; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setupSearchView]; [self setupBannerView]; [self setupTableView]; self.index=0; [self requestBanner]; [self requestSource]; } - (void)setupSearchView { UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.navigationController.navigationBar.bounds.size.height)]; topView.backgroundColor = [UIColor whiteColor]; self.navigationItem.titleView = topView; self.searchTextField = [[UITextField alloc] initWithFrame:CGRectZero]; self.searchTextField.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8]; self.searchTextField.delegate = self; self.searchTextField.layer.cornerRadius = 14; self.searchTextField.font = FONT_SYS(14); self.searchTextField.placeholder = @"搜索你想要了解的资讯"; [self.searchTextField setValue:FONT_SYS(14) forKeyPath:@"_placeholderLabel.font"]; [topView addSubview:self.searchTextField]; UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(14, 1, 36, 22)]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 1, 22, 22)]; imgView.image = [UIImage imageNamed:@"main_search"]; [imageView addSubview:imgView]; self.searchTextField.leftView = imageView; self.searchTextField.leftViewMode = UITextFieldViewModeAlways; self.searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchTextFieldAction)]; [self.searchTextField addGestureRecognizer:tap]; UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [customBtn setImage:[UIImage imageNamed:@"main_custom"] forState:UIControlStateNormal]; [customBtn addTarget:self action:@selector(customButtonAction) forControlEvents:UIControlEventTouchUpInside]; [topView addSubview:customBtn]; [customBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(topView).offset(-6); make.centerY.equalTo(topView); make.size.mas_equalTo(CGSizeMake(22, 22)); }]; [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(topView).offset(6); make.centerY.equalTo(topView); make.size.mas_equalTo(CGSizeMake(310*SCREEN_MUTI, 28)); }]; } - (void)searchTextFieldAction { CATransition * animation = [CATransition animation]; animation.duration = 0.5; // 时间 animation.type = kCATransitionFade; animation.subtype = kCATransitionFromTop; [self.view.window.layer addAnimation:animation forKey:nil]; KXNewKeySearchViewController *search=[[KXNewKeySearchViewController alloc]init]; [self.navigationController pushViewController:search animated:YES]; } - (void)customButtonAction { KXCustomServiceViewController *vc = [[KXCustomServiceViewController alloc] init]; [self.navigationController pushViewController:vc animated:YES]; } - (void)setupBannerView { self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (150+180+12+55)*SCREEN_MUTI)]; self.headerView.backgroundColor = [UIColor whiteColor]; self.bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150*SCREEN_MUTI)]; self.bannerImageView.backgroundColor = [UIColor whiteColor]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerImageViewAction)]; [self.bannerImageView addGestureRecognizer:tap]; self.bannerImageView.userInteractionEnabled = YES; [self.headerView addSubview:self.bannerImageView]; UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; flow.itemSize = CGSizeMake(100*SCREEN_MUTI, 69*SCREEN_MUTI); flow.sectionInset = UIEdgeInsetsMake(14*SCREEN_MUTI, 14*SCREEN_MUTI, 14*SCREEN_MUTI, 14*SCREEN_MUTI); flow.minimumInteritemSpacing = (SCREEN_WIDTH-36*SCREEN_MUTI*2-100*SCREEN_MUTI*3)/2; flow.minimumLineSpacing = 14*SCREEN_MUTI; self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150*SCREEN_MUTI, SCREEN_WIDTH, 180*SCREEN_MUTI) collectionViewLayout:flow]; self.collectionView.backgroundColor = [UIColor whiteColor]; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.collectionView registerClass:[KXBannerCollectionViewCell class] forCellWithReuseIdentifier:@"banner"]; [self.headerView addSubview:self.collectionView]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, (150+180)*SCREEN_MUTI, SCREEN_WIDTH, 12*SCREEN_MUTI)]; line.backgroundColor = [UIColor KXColorWithHex:0xf5f5f5]; [self.headerView addSubview:line]; _optionView = [[KXOptionButtonView alloc] initWithFrame:CGRectMake(0, (150+180+12)*SCREEN_MUTI, SCREEN_WIDTH, 55*SCREEN_MUTI)]; _optionView.selectedColor=[UIColor baseColor]; _optionView.nomalColor=[UIColor titleColor]; _optionView.titleFont=FONT_SYS(14); _optionView.itemSize=CGSizeMake(SCREEN_WIDTH/6, 55*SCREEN_MUTI); [self.headerView addSubview:_optionView]; __weak typeof(self) weakSelf=self; _optionView.operation = ^(NSInteger index) { // CGFloat contentOffsetX = self.view.frame.size.width * index; weakSelf.index=index; // UITableView *tableView=(UITableView *)[weakSelf.scrollView viewWithTag:index+1024]; [weakSelf.tableView.mj_header beginRefreshing]; // weakSelf.scrollView.contentOffset = CGPointMake(contentOffsetX, 0); }; UILabel *hLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 54.5*SCREEN_MUTI, SCREEN_WIDTH, 0.5)]; hLabel.backgroundColor=[UIColor detailTitleColor]; [_optionView addSubview:hLabel]; } - (void)bannerImageViewAction { KXNewsWebViewController *web=[[KXNewsWebViewController alloc]init]; web.webID=_bannerID; [self.navigationController pushViewController:web animated:YES]; } - (void)setupTableView { self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight-TabbarHeight) style:UITableViewStylePlain]; if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { } self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.tableHeaderView = self.headerView; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.tableView registerClass:[KXNewsTableViewCell class] forCellReuseIdentifier:@"news"]; [self.view addSubview:self.tableView]; //下拉刷新 MJRefreshNormalHeader *header = [[MJRefreshNormalHeader alloc]init]; [header setRefreshingTarget:self refreshingAction:@selector(refreshWithHeader:)]; self.tableView.mj_header = header; //上拉加载 MJRefreshBackNormalFooter *footer=[[MJRefreshBackNormalFooter alloc]init]; [footer setRefreshingTarget:self refreshingAction:@selector(refreshFooter:)]; self.tableView.mj_footer=footer; header.stateLabel.font = FONT_SYS(14); header.stateLabel.textColor=[UIColor detailTitleColor]; header.lastUpdatedTimeLabel.font=FONT_SYS(14); header.lastUpdatedTimeLabel.textColor=[UIColor detailTitleColor]; footer.stateLabel.textColor=[UIColor detailTitleColor]; } - (void)requestBanner { NSString *urlString=[NSString stringWithFormat:@"%@/information/banner",URL]; [KXHTTP post:urlString params:nil success:^(id json) { [self.bannerImageView sd_setImageWithURL:[NSURL URLWithString:json[@"ios_banner"][@"thumbnail"]]]; _bannerID = json[@"ios_banner"][@"id"]; } failure:^(NSError *error) { }]; } -(void)requestSource { NSString *urlString=[NSString stringWithFormat:@"%@/information/getclassification",URL]; [KXHTTP post:urlString params:@{@"package_id":@"package_5"} success:^(id json) { if ([json isKindOfClass:[NSNull class]]) { return ; } _titleArray=[[NSMutableArray alloc]init];; for (int i=0 ; i<[json count]; i++) { [_titleArray addObject:json[i]]; } _optionView.titleArray=_titleArray; _optionView.maxVisible=_titleArray.count; // [self setScrollView]; [self.tableView.mj_header beginRefreshing]; } failure:^(NSError *error) { }]; } #pragma mark 设置主体滚动视图 - (void)setScrollView { // 创建滚动视图 UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:scrollView]; [scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view); make.bottom.equalTo(self.view); make.top.mas_equalTo(self.view).offset(150+180+12+55); make.right.equalTo(self.view); }]; self.scrollView = scrollView; self.scrollView.tag=1234; NSUInteger count = _titleArray.count; // 给滚动视图添加内容 for (NSUInteger i = 0; i