Ei kuvausta

KXNewMainPageViewController.m 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. //
  2. // KXNewMainPageViewController.m
  3. // CAISHEN
  4. //
  5. // Created by xiaoxi on 2017/12/5.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXNewMainPageViewController.h"
  9. #import "KXOptionButtonView.h"
  10. #import "KXNewsTableViewCell.h"
  11. #import "KXNewsWebViewController.h"
  12. #import "KXBannerCollectionViewCell.h"
  13. #import "KXHouseCalculateViewController.h"
  14. #import "KXEqualRateToolViewController.h"
  15. #import "KXIndividualIncomeViewController.h"
  16. #import "KXInstallmentViewController.h"
  17. #import "KXCarLoancalViewController.h"
  18. #import "KXBaseLinkViewController.h"
  19. #import "KXNewKeySearchViewController.h"
  20. #import "KXCustomServiceViewController.h"
  21. @interface KXNewMainPageViewController () <UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  22. @property (nonatomic, strong) UITableView *tableView;
  23. @property (nonatomic, strong) UITextField *searchTextField;
  24. @property (nonatomic, strong) UIImageView *bannerImageView;
  25. @property (nonatomic, strong) UIView *headerView;
  26. @property (nonatomic, strong) UICollectionView *collectionView;
  27. @property (nonatomic, strong) NSMutableArray *tableViewDataSource;
  28. @property (nonatomic, strong) NSArray *collectionViewDataSource;
  29. @property (nonatomic, assign) NSInteger page;
  30. @property (nonatomic, assign) NSInteger index;
  31. @property (nonatomic, strong) KXOptionButtonView *optionView;
  32. @property (nonatomic, strong) NSMutableArray *titleArray;
  33. @property (nonatomic, strong) UIScrollView *scrollView;
  34. @end
  35. @implementation KXNewMainPageViewController {
  36. NSString *_bannerID;
  37. }
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. // Do any additional setup after loading the view.
  41. [self setupSearchView];
  42. [self setupBannerView];
  43. [self setupTableView];
  44. self.index=0;
  45. [self requestBanner];
  46. [self requestSource];
  47. }
  48. - (void)setupSearchView {
  49. UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.navigationController.navigationBar.bounds.size.height)];
  50. topView.backgroundColor = [UIColor whiteColor];
  51. self.navigationItem.titleView = topView;
  52. self.searchTextField = [[UITextField alloc] initWithFrame:CGRectZero];
  53. self.searchTextField.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8];
  54. self.searchTextField.delegate = self;
  55. self.searchTextField.layer.cornerRadius = 14;
  56. self.searchTextField.font = FONT_SYS(14);
  57. self.searchTextField.placeholder = @"搜索你想要了解的资讯";
  58. [self.searchTextField setValue:FONT_SYS(14) forKeyPath:@"_placeholderLabel.font"];
  59. [topView addSubview:self.searchTextField];
  60. UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(14, 1, 36, 22)];
  61. UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 1, 22, 22)];
  62. imgView.image = [UIImage imageNamed:@"main_search"];
  63. [imageView addSubview:imgView];
  64. self.searchTextField.leftView = imageView;
  65. self.searchTextField.leftViewMode = UITextFieldViewModeAlways;
  66. self.searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  67. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchTextFieldAction)];
  68. [self.searchTextField addGestureRecognizer:tap];
  69. UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  70. [customBtn setImage:[UIImage imageNamed:@"main_custom"] forState:UIControlStateNormal];
  71. [customBtn addTarget:self action:@selector(customButtonAction) forControlEvents:UIControlEventTouchUpInside];
  72. [topView addSubview:customBtn];
  73. [customBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.right.equalTo(topView).offset(-6);
  75. make.centerY.equalTo(topView);
  76. make.size.mas_equalTo(CGSizeMake(22, 22));
  77. }];
  78. [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
  79. make.left.equalTo(topView).offset(6);
  80. make.centerY.equalTo(topView);
  81. make.size.mas_equalTo(CGSizeMake(310*SCREEN_MUTI, 28));
  82. }];
  83. }
  84. - (void)searchTextFieldAction {
  85. CATransition * animation = [CATransition animation];
  86. animation.duration = 0.5; // 时间
  87. animation.type = kCATransitionFade;
  88. animation.subtype = kCATransitionFromTop;
  89. [self.view.window.layer addAnimation:animation forKey:nil];
  90. KXNewKeySearchViewController *search=[[KXNewKeySearchViewController alloc]init];
  91. [self.navigationController pushViewController:search animated:YES];
  92. }
  93. - (void)customButtonAction {
  94. KXCustomServiceViewController *vc = [[KXCustomServiceViewController alloc] init];
  95. [self.navigationController pushViewController:vc animated:YES];
  96. }
  97. - (void)setupBannerView {
  98. self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (150+180+12+55)*SCREEN_MUTI)];
  99. self.headerView.backgroundColor = [UIColor whiteColor];
  100. self.bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150*SCREEN_MUTI)];
  101. self.bannerImageView.backgroundColor = [UIColor whiteColor];
  102. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerImageViewAction)];
  103. [self.bannerImageView addGestureRecognizer:tap];
  104. self.bannerImageView.userInteractionEnabled = YES;
  105. [self.headerView addSubview:self.bannerImageView];
  106. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
  107. flow.itemSize = CGSizeMake(100*SCREEN_MUTI, 69*SCREEN_MUTI);
  108. flow.sectionInset = UIEdgeInsetsMake(14*SCREEN_MUTI, 14*SCREEN_MUTI, 14*SCREEN_MUTI, 14*SCREEN_MUTI);
  109. flow.minimumInteritemSpacing = (SCREEN_WIDTH-36*SCREEN_MUTI*2-100*SCREEN_MUTI*3)/2;
  110. flow.minimumLineSpacing = 14*SCREEN_MUTI;
  111. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150*SCREEN_MUTI, SCREEN_WIDTH, 180*SCREEN_MUTI) collectionViewLayout:flow];
  112. self.collectionView.backgroundColor = [UIColor whiteColor];
  113. self.collectionView.delegate = self;
  114. self.collectionView.dataSource = self;
  115. [self.collectionView registerClass:[KXBannerCollectionViewCell class] forCellWithReuseIdentifier:@"banner"];
  116. [self.headerView addSubview:self.collectionView];
  117. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, (150+180)*SCREEN_MUTI, SCREEN_WIDTH, 12*SCREEN_MUTI)];
  118. line.backgroundColor = [UIColor KXColorWithHex:0xf5f5f5];
  119. [self.headerView addSubview:line];
  120. _optionView = [[KXOptionButtonView alloc] initWithFrame:CGRectMake(0, (150+180+12)*SCREEN_MUTI, SCREEN_WIDTH, 55*SCREEN_MUTI)];
  121. _optionView.selectedColor=[UIColor baseColor];
  122. _optionView.nomalColor=[UIColor titleColor];
  123. _optionView.titleFont=FONT_SYS(14);
  124. _optionView.itemSize=CGSizeMake(SCREEN_WIDTH/6, 55*SCREEN_MUTI);
  125. [self.headerView addSubview:_optionView];
  126. __weak typeof(self) weakSelf=self;
  127. _optionView.operation = ^(NSInteger index) {
  128. // CGFloat contentOffsetX = self.view.frame.size.width * index;
  129. weakSelf.index=index;
  130. // UITableView *tableView=(UITableView *)[weakSelf.scrollView viewWithTag:index+1024];
  131. [weakSelf.tableView.mj_header beginRefreshing];
  132. // weakSelf.scrollView.contentOffset = CGPointMake(contentOffsetX, 0);
  133. };
  134. UILabel *hLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 54.5*SCREEN_MUTI, SCREEN_WIDTH, 0.5)];
  135. hLabel.backgroundColor=[UIColor detailTitleColor];
  136. [_optionView addSubview:hLabel];
  137. }
  138. - (void)bannerImageViewAction {
  139. KXNewsWebViewController *web=[[KXNewsWebViewController alloc]init];
  140. web.webID=_bannerID;
  141. [self.navigationController pushViewController:web animated:YES];
  142. }
  143. - (void)setupTableView {
  144. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight-TabbarHeight) style:UITableViewStylePlain];
  145. if (@available(iOS 11.0, *)) {
  146. self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  147. } else {
  148. }
  149. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  150. self.tableView.tableHeaderView = self.headerView;
  151. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  152. self.tableView.delegate = self;
  153. self.tableView.dataSource = self;
  154. [self.tableView registerClass:[KXNewsTableViewCell class] forCellReuseIdentifier:@"news"];
  155. [self.view addSubview:self.tableView];
  156. //下拉刷新
  157. MJRefreshNormalHeader *header =
  158. [[MJRefreshNormalHeader alloc]init];
  159. [header setRefreshingTarget:self refreshingAction:@selector(refreshWithHeader:)];
  160. self.tableView.mj_header = header;
  161. //上拉加载
  162. MJRefreshBackNormalFooter *footer=[[MJRefreshBackNormalFooter alloc]init];
  163. [footer setRefreshingTarget:self refreshingAction:@selector(refreshFooter:)];
  164. self.tableView.mj_footer=footer;
  165. header.stateLabel.font = FONT_SYS(14);
  166. header.stateLabel.textColor=[UIColor detailTitleColor];
  167. header.lastUpdatedTimeLabel.font=FONT_SYS(14);
  168. header.lastUpdatedTimeLabel.textColor=[UIColor detailTitleColor];
  169. footer.stateLabel.textColor=[UIColor detailTitleColor];
  170. }
  171. - (void)requestBanner {
  172. NSString *urlString=[NSString stringWithFormat:@"%@/information/banner",URL];
  173. [KXHTTP post:urlString params:nil success:^(id json) {
  174. [self.bannerImageView sd_setImageWithURL:[NSURL URLWithString:json[@"ios_banner"][@"thumbnail"]]];
  175. _bannerID = json[@"ios_banner"][@"id"];
  176. } failure:^(NSError *error) {
  177. }];
  178. }
  179. -(void)requestSource
  180. {
  181. NSString *urlString=[NSString stringWithFormat:@"%@/information/getclassification",URL];
  182. [KXHTTP post:urlString params:@{@"package_id":@"package_5"} success:^(id json) {
  183. if ([json isKindOfClass:[NSNull class]]) {
  184. return ;
  185. }
  186. _titleArray=[[NSMutableArray alloc]init];;
  187. for (int i=0 ; i<[json count]; i++) {
  188. [_titleArray addObject:json[i]];
  189. }
  190. _optionView.titleArray=_titleArray;
  191. _optionView.maxVisible=_titleArray.count;
  192. // [self setScrollView];
  193. [self.tableView.mj_header beginRefreshing];
  194. } failure:^(NSError *error) {
  195. }];
  196. }
  197. #pragma mark 设置主体滚动视图
  198. - (void)setScrollView {
  199. // 创建滚动视图
  200. UIScrollView *scrollView = [[UIScrollView alloc] init];
  201. scrollView.backgroundColor = [UIColor whiteColor];
  202. [self.view addSubview:scrollView];
  203. [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  204. make.left.equalTo(self.view);
  205. make.bottom.equalTo(self.view);
  206. make.top.mas_equalTo(self.view).offset(150+180+12+55);
  207. make.right.equalTo(self.view);
  208. }];
  209. self.scrollView = scrollView;
  210. self.scrollView.tag=1234;
  211. NSUInteger count = _titleArray.count;
  212. // 给滚动视图添加内容
  213. for (NSUInteger i = 0; i<count; i++) {
  214. UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-49-215) style:UITableViewStylePlain];
  215. tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
  216. [tableView registerClass:[KXNewsTableViewCell class] forCellReuseIdentifier:@"news"];
  217. tableView.tableFooterView=[[UIView alloc]init];
  218. tableView.tag = 1024 + i;
  219. //下拉刷新
  220. MJRefreshNormalHeader *header =
  221. [[MJRefreshNormalHeader alloc]init];
  222. [header setRefreshingTarget:self refreshingAction:@selector(refreshWithHeader:)];
  223. tableView.mj_header = header;
  224. //上拉加载
  225. MJRefreshAutoNormalFooter *footer=[[MJRefreshAutoNormalFooter alloc]init];
  226. [footer setRefreshingTarget:self refreshingAction:@selector(refreshFooter:)];
  227. tableView.mj_footer=footer;
  228. tableView.delegate = self;
  229. tableView.dataSource = self;
  230. [_scrollView addSubview:tableView];
  231. header.stateLabel.font = FONT_SYS(14);
  232. header.stateLabel.textColor=[UIColor detailTitleColor];
  233. header.lastUpdatedTimeLabel.font=FONT_SYS(14);
  234. header.lastUpdatedTimeLabel.textColor=[UIColor detailTitleColor];
  235. footer.stateLabel.textColor=[UIColor detailTitleColor];
  236. }
  237. //加载第一页
  238. self.index=0;
  239. UITableView *tableView=(UITableView *)[self.scrollView viewWithTag:1024];
  240. [tableView.mj_header beginRefreshing];
  241. // 设置滚动视图的其他属性
  242. scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * count, scrollView.frame.size.height);
  243. scrollView.bounces = NO;
  244. scrollView.pagingEnabled = YES;
  245. scrollView.showsHorizontalScrollIndicator = NO;
  246. scrollView.showsVerticalScrollIndicator = NO;
  247. scrollView.delegate = self;
  248. }
  249. #pragma mark 下拉刷新和上拉加载
  250. -(void)refreshWithHeader:(MJRefreshHeader *)header
  251. {
  252. self.page=1;
  253. [self requestDataSourceWithPage:self.page];
  254. }
  255. -(void)refreshFooter:(MJRefreshAutoFooter *)footer
  256. {
  257. self.page++;
  258. [self requestDataSourceWithPage:self.page];
  259. }
  260. #pragma mark 请求数据
  261. -(void)requestDataSourceWithPage:(NSInteger)page
  262. {
  263. NSString *urlString=[NSString stringWithFormat:@"%@/information/getinformationsbypackage",URL];
  264. NSDictionary *paraDict=@{@"package_id":@"package_5",@"classification_id":_titleArray[self.index],@"page":@(page)};
  265. [KXHTTP post:urlString params:paraDict success:^(id json) {
  266. if (self.page==1) {
  267. [self.tableViewDataSource removeAllObjects];
  268. }
  269. NSArray *array=[NSArray yy_modelArrayWithClass:[KXNewsModel class] json:[json[@"listdata"] valueForKey:@"data"]];
  270. @synchronized (self.tableViewDataSource) {
  271. for (KXNewsModel *model in array) {
  272. [self.tableViewDataSource addObject:model];
  273. }
  274. }
  275. // UITableView *tableView=(UITableView *)[self.scrollView viewWithTag:self.index+1024];
  276. [self.tableView.mj_header endRefreshing];
  277. [self.tableView.mj_footer endRefreshing];
  278. [self.tableView reloadData];
  279. } failure:^(NSError *error) {
  280. }];
  281. }
  282. #pragma mark tableviewDelagete,DataSource
  283. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  284. {
  285. return self.tableViewDataSource.count;
  286. }
  287. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  288. {
  289. KXNewsModel *model=self.tableViewDataSource[indexPath.row];
  290. KXNewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"news"];
  291. if (!cell) {
  292. cell=[[KXNewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"news"];
  293. }
  294. cell.selectionStyle=UITableViewCellSelectionStyleNone;
  295. cell.newsModel=model;
  296. return cell;
  297. }
  298. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  299. {
  300. return 100;
  301. }
  302. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  303. {
  304. KXNewsModel *model=self.tableViewDataSource[indexPath.row];
  305. KXNewsWebViewController *web=[[KXNewsWebViewController alloc]init];
  306. web.webID=model.Id;
  307. [self.navigationController pushViewController:web animated:YES];
  308. }
  309. #pragma mark collectiondelegate
  310. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  311. return self.collectionViewDataSource.count;
  312. }
  313. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  314. KXBannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"banner" forIndexPath:indexPath];
  315. NSDictionary *dict = self.collectionViewDataSource[indexPath.item];
  316. [cell setTitle:dict[@"name"] Image:dict[@"image"]];
  317. return cell;
  318. }
  319. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  320. {
  321. if (indexPath.item == 0) {
  322. KXHouseCalculateViewController *houseCalcul = [[KXHouseCalculateViewController alloc] init];
  323. [self.navigationController pushViewController:houseCalcul animated:YES];
  324. }
  325. if (indexPath.item == 1) {
  326. KXInstallmentViewController *vc = [[KXInstallmentViewController alloc] init];
  327. [self.navigationController pushViewController:vc animated:YES];
  328. }
  329. if (indexPath.item == 2) {
  330. KXCarLoancalViewController *carloanVC = [[KXCarLoancalViewController alloc]init];
  331. [self.navigationController pushViewController:carloanVC animated:YES];
  332. }
  333. if (indexPath.item == 3) {
  334. KXEqualRateToolViewController *equRateTool = [[KXEqualRateToolViewController alloc] init];
  335. equRateTool.equalType = EqualMoneyType;
  336. [self.navigationController pushViewController:equRateTool animated:YES];
  337. }
  338. if (indexPath.item == 4) {
  339. KXEqualRateToolViewController *equalRate = [[KXEqualRateToolViewController alloc] init];
  340. equalRate.equalType = EqualRateType;
  341. [self.navigationController pushViewController:equalRate animated:YES];
  342. }
  343. if (indexPath.item == 5) {
  344. KXIndividualIncomeViewController *vc = [[KXIndividualIncomeViewController alloc] init];
  345. [self.navigationController pushViewController:vc animated:YES];
  346. }
  347. }
  348. #pragma mark - scrollView代理
  349. -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  350. {
  351. NSUInteger page = scrollView.contentOffset.x / scrollView.frame.size.width;
  352. if (scrollView.tag==1234) {
  353. UITableView *tableView=(UITableView *)[self.scrollView viewWithTag:page+1024];
  354. // 四舍五入计算出页码
  355. if (page!=self.index) {
  356. [_optionView selectedBtnAtIndex:(int)(page+0.5)];
  357. [tableView.mj_header beginRefreshing];
  358. }
  359. self.index=page;
  360. }
  361. }
  362. #pragma mark textField delegate
  363. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  364. return NO;
  365. }
  366. - (NSMutableArray *)titleArray {
  367. if (!_titleArray) {
  368. _titleArray = [NSMutableArray array];
  369. }
  370. return _titleArray;
  371. }
  372. - (NSMutableArray *)tableViewDataSource {
  373. if (!_tableViewDataSource) {
  374. _tableViewDataSource = [NSMutableArray array];
  375. }
  376. return _tableViewDataSource;
  377. }
  378. - (NSArray *)collectionViewDataSource {
  379. if (!_collectionViewDataSource) {
  380. _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":@"个税计算"}];
  381. }
  382. return _collectionViewDataSource;
  383. }
  384. - (void)didReceiveMemoryWarning {
  385. [super didReceiveMemoryWarning];
  386. // Dispose of any resources that can be recreated.
  387. }
  388. /*
  389. #pragma mark - Navigation
  390. // In a storyboard-based application, you will often want to do a little preparation before navigation
  391. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  392. // Get the new view controller using [segue destinationViewController].
  393. // Pass the selected object to the new view controller.
  394. }
  395. */
  396. @end