123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- //
- // 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 () <UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
- @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<count; i++) {
- UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-49-215) style:UITableViewStylePlain];
- tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
- [tableView registerClass:[KXNewsTableViewCell class] forCellReuseIdentifier:@"news"];
- tableView.tableFooterView=[[UIView alloc]init];
- tableView.tag = 1024 + i;
- //下拉刷新
- MJRefreshNormalHeader *header =
- [[MJRefreshNormalHeader alloc]init];
- [header setRefreshingTarget:self refreshingAction:@selector(refreshWithHeader:)];
- tableView.mj_header = header;
- //上拉加载
- MJRefreshAutoNormalFooter *footer=[[MJRefreshAutoNormalFooter alloc]init];
- [footer setRefreshingTarget:self refreshingAction:@selector(refreshFooter:)];
- tableView.mj_footer=footer;
- tableView.delegate = self;
- tableView.dataSource = self;
- [_scrollView addSubview:tableView];
-
- 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];
-
- }
- //加载第一页
- self.index=0;
-
- UITableView *tableView=(UITableView *)[self.scrollView viewWithTag:1024];
- [tableView.mj_header beginRefreshing];
- // 设置滚动视图的其他属性
- scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * count, scrollView.frame.size.height);
- scrollView.bounces = NO;
- scrollView.pagingEnabled = YES;
- scrollView.showsHorizontalScrollIndicator = NO;
- scrollView.showsVerticalScrollIndicator = NO;
- scrollView.delegate = self;
- }
- #pragma mark 下拉刷新和上拉加载
- -(void)refreshWithHeader:(MJRefreshHeader *)header
- {
- self.page=1;
- [self requestDataSourceWithPage:self.page];
- }
- -(void)refreshFooter:(MJRefreshAutoFooter *)footer
- {
- self.page++;
- [self requestDataSourceWithPage:self.page];
- }
- #pragma mark 请求数据
- -(void)requestDataSourceWithPage:(NSInteger)page
- {
- NSString *urlString=[NSString stringWithFormat:@"%@/information/getinformationsbypackage",URL];
- NSDictionary *paraDict=@{@"package_id":@"package_5",@"classification_id":_titleArray[self.index],@"page":@(page)};
-
- [KXHTTP post:urlString params:paraDict success:^(id json) {
- if (self.page==1) {
- [self.tableViewDataSource removeAllObjects];
- }
-
- NSArray *array=[NSArray yy_modelArrayWithClass:[KXNewsModel class] json:[json[@"listdata"] valueForKey:@"data"]];
- @synchronized (self.tableViewDataSource) {
- for (KXNewsModel *model in array) {
- [self.tableViewDataSource addObject:model];
- }
- }
- // UITableView *tableView=(UITableView *)[self.scrollView viewWithTag:self.index+1024];
- [self.tableView.mj_header endRefreshing];
- [self.tableView.mj_footer endRefreshing];
- [self.tableView reloadData];
-
- } failure:^(NSError *error) {
-
- }];
- }
- #pragma mark tableviewDelagete,DataSource
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
-
- return self.tableViewDataSource.count;
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
-
- KXNewsModel *model=self.tableViewDataSource[indexPath.row];
- KXNewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"news"];
- if (!cell) {
- cell=[[KXNewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"news"];
- }
- cell.selectionStyle=UITableViewCellSelectionStyleNone;
- cell.newsModel=model;
- return cell;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 100;
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- KXNewsModel *model=self.tableViewDataSource[indexPath.row];
-
- KXNewsWebViewController *web=[[KXNewsWebViewController alloc]init];
-
- web.webID=model.Id;
-
- [self.navigationController pushViewController:web animated:YES];
- }
- #pragma mark collectiondelegate
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
-
- return self.collectionViewDataSource.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- KXBannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"banner" forIndexPath:indexPath];
- NSDictionary *dict = self.collectionViewDataSource[indexPath.item];
- [cell setTitle:dict[@"name"] Image:dict[@"image"]];
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- if (indexPath.item == 0) {
- KXHouseCalculateViewController *houseCalcul = [[KXHouseCalculateViewController alloc] init];
- [self.navigationController pushViewController:houseCalcul animated:YES];
- }
-
- if (indexPath.item == 1) {
- KXInstallmentViewController *vc = [[KXInstallmentViewController alloc] init];
- [self.navigationController pushViewController:vc animated:YES];
- }
-
- if (indexPath.item == 2) {
- KXCarLoancalViewController *carloanVC = [[KXCarLoancalViewController alloc]init];
- [self.navigationController pushViewController:carloanVC animated:YES];
- }
-
- if (indexPath.item == 3) {
- KXEqualRateToolViewController *equRateTool = [[KXEqualRateToolViewController alloc] init];
- equRateTool.equalType = EqualMoneyType;
- [self.navigationController pushViewController:equRateTool animated:YES];
- }
-
-
- if (indexPath.item == 4) {
- KXEqualRateToolViewController *equalRate = [[KXEqualRateToolViewController alloc] init];
- equalRate.equalType = EqualRateType;
- [self.navigationController pushViewController:equalRate animated:YES];
- }
-
- if (indexPath.item == 5) {
- KXIndividualIncomeViewController *vc = [[KXIndividualIncomeViewController alloc] init];
- [self.navigationController pushViewController:vc animated:YES];
- }
- }
- #pragma mark - scrollView代理
- -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- NSUInteger page = scrollView.contentOffset.x / scrollView.frame.size.width;
- if (scrollView.tag==1234) {
- UITableView *tableView=(UITableView *)[self.scrollView viewWithTag:page+1024];
- // 四舍五入计算出页码
- if (page!=self.index) {
- [_optionView selectedBtnAtIndex:(int)(page+0.5)];
- [tableView.mj_header beginRefreshing];
- }
- self.index=page;
-
- }
-
- }
- #pragma mark textField delegate
- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
-
- return NO;
- }
- - (NSMutableArray *)titleArray {
- if (!_titleArray) {
- _titleArray = [NSMutableArray array];
- }
- return _titleArray;
- }
- - (NSMutableArray *)tableViewDataSource {
- if (!_tableViewDataSource) {
- _tableViewDataSource = [NSMutableArray array];
- }
- return _tableViewDataSource;
- }
- - (NSArray *)collectionViewDataSource {
- if (!_collectionViewDataSource) {
- _collectionViewDataSource = @[@{@"image":@"main_house",@"name":@"房贷计算"},@{@"image":@"main_card",@"name":@"信用卡分期"},@{@"image":@"main_car",@"name":@"车贷计算"},@{@"image":@"main_benjin",@"name":@"等额本金计算"},@{@"image":@"main_benxi",@"name":@"等额本息计算"},@{@"image":@"main_mine",@"name":@"个税计算"}];
- }
- return _collectionViewDataSource;
- }
- - (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
|