// // ViewController.m // chinaTV // // Created by 刘伟伟 on 2023/7/5. // #import "ViewController.h" #import "HomeCommonBarView.h" #import "CTPageControl.h" #import "ZFNormalViewController.h" #import "SearchCommonBarView.h" #import "SearchResultViewController.h" #import @interface ViewController () @property(nonatomic,strong)HomeCommonBarView *customerbar; @property(nonatomic,strong)SearchCommonBarView *homeSearchBar; @property(nonatomic,strong)UITableView *tableView; @property(nonatomic,strong)NSDictionary *homePageData; @property(nonatomic,strong)NSMutableArray *todayCommondData; @property(nonatomic,assign)int vid; @property(nonatomic,assign)int tb1footpage; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.tb1footpage = 1; self.todayCommondData = [NSMutableArray array]; [self setupUI]; [self requestHomePageData]; } -(void)requestHomePageData{ if(![UserModel userToken]){ [RequestTool post:@"user/userLogin" params:@{@"account":@"test",@"password":@"123456"} success:^(id responseObject) { NSLog(@"%@",responseObject); [[NSUserDefaults standardUserDefaults] setValue:responseObject[@"data"] forKey:@"existUserData"]; [self requestHomePageData2]; } failure:^(NSError *error) { NSLog(@"%@",error); }]; }else{ [self requestHomePageData2]; } } -(void)todayReCommond{ //今日推荐 if(self.tb1footpage>10){ [self.tableView.mj_footer endRefreshing]; return; } [RequestTool post:@"home/dailyRecommend" params:@{@"page":@{@"page":@(self.tb1footpage),@"pageSize":@(9)}} success:^(id responseObject) { NSLog(@"home/dailyRecommend-%@",responseObject); if([responseObject[@"code"] intValue] == 0){ NSArray *temp = responseObject[@"data"][@"list"]; if(![temp isKindOfClass:[NSNull class]] && temp.count>0){ [self.todayCommondData addObjectsFromArray:temp]; // [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone]; [self.tableView reloadData]; } [self.tableView.mj_footer endRefreshing]; } } failure:^(NSError *error) { [self.tableView.mj_footer endRefreshing]; }]; } -(void)homeSearchWithKeyword:(NSString *)keyword{ NSString *trimmed = [[keyword stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] copy]; if(trimmed.length == 0){ return; } [self.view endEditing:YES]; NSMutableArray *history = [NSMutableArray array]; id storedHistory = [[NSUserDefaults standardUserDefaults] objectForKey:@"mylocationhistory"]; if([storedHistory isKindOfClass:[NSArray class]]){ [history addObjectsFromArray:(NSArray *)storedHistory]; } if(![history containsObject:trimmed]){ [history addObject:trimmed]; [[NSUserDefaults standardUserDefaults] setObject:history forKey:@"mylocationhistory"]; } SearchResultViewController *vc = [SearchResultViewController new]; vc.txt = trimmed; vc.hidesBottomBarWhenPushed = true; [self.navigationController pushViewController:vc animated:true]; } -(void)requestHomePageData2{ //首页数据 [RequestTool post:@"home/homePage" params:@{} success:^(id responseObject) { NSLog(@"home/homePage-%@",responseObject); if([responseObject[@"code"] intValue] == 0){ self.homePageData = responseObject[@"data"]; [self.tableView reloadData]; } } failure:^(NSError *error) {}]; [self todayReCommond]; // 推荐相关逻辑已移除 } -(void)setupUI{ self.fd_prefersNavigationBarHidden = true; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view); }]; [self.tableView reloadData]; [self.view addSubview:self.customerbar]; [self.view bringSubviewToFront:self.customerbar]; self.homeSearchBar.frame = self.customerbar.bounds; self.homeSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self.customerbar addSubview:self.homeSearchBar]; __weak typeof (self) weakSelf = self; self.customerbar.changeTitAction = nil; self.customerbar.changeActionToFirst = ^{ CGPoint point=weakSelf.tableView.contentOffset; if(point.y<100){ weakSelf.customerbar.imageView.hidden = true; weakSelf.customerbar.bgView.backgroundColor = [UIColor clearColor]; weakSelf.homeSearchBar.backgroundColor = [UIColor clearColor]; } }; } -(UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent; } //监听scrollView滑动 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == self.tableView) { CGFloat offsetY = scrollView.contentOffset.y; // NSLog(@"%f",offsetY); if(offsetY > 100 && self.customerbar.imageView.isHidden){ self.customerbar.imageView.hidden = false; self.customerbar.bgView.backgroundColor = rgba(18, 23, 41, 1); self.homeSearchBar.backgroundColor = rgba(18, 23, 41, 1); } if(offsetY<=100 && !self.customerbar.imageView.isHidden){ self.customerbar.imageView.hidden = true; self.customerbar.bgView.backgroundColor = [UIColor clearColor]; self.homeSearchBar.backgroundColor = [UIColor clearColor]; } } } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if(tableView == self.tableView){ if(section == 0){ return 0; }else{ return 54; } } return 0; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.01; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ if(tableView == self.tableView){ if(section == 1){ headerView *header = [[headerView alloc] init]; header.title = @"热门影视"; return header; }else if(section == 2){ headerView *header = [[headerView alloc] init]; header.title = @"今日推荐"; return header; } } return [UIView new]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ if(tableView == self.tableView){ return 3; } return 0; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if(tableView == self.tableView){ if(section == 2){ if(self.todayCommondData.count%3==0){ return self.todayCommondData.count/3; }else{ return self.todayCommondData.count/3 + 1; } }else{ return 1; } } return 0; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(tableView == self.tableView){ if(indexPath.section == 0){ return kScreenWidth*340/375; }else if(indexPath.section ==1){ return 268; }else { return 214; } } return 0; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ __weak typeof (self) weakSelf = self; if(tableView == self.tableView){ if(indexPath.section == 0){ homecell1 *cell = [homecell1 cellWithTableView:tableView]; cell.banner = self.homePageData[@"banners"]; cell.tapBlock = ^(int index) { weakSelf.vid = index; [weakSelf playItem]; }; return cell; }else if(indexPath.section == 1){ homecell2 *cell = [homecell2 cellWithTableView:tableView]; cell.hotmovies = self.homePageData[@"hot_movies"]; cell.tapBlock = ^(int index) { weakSelf.vid = index; [weakSelf playItem]; }; return cell; }else{ homecell3 *cell = [homecell3 cellWithTableView:tableView]; if(indexPath.row*3 + 3>self.todayCommondData.count){ cell.todayCommond = [self.todayCommondData subarrayWithRange:NSMakeRange(indexPath.row*3, self.todayCommondData.count-indexPath.row*3)]; }else{ cell.todayCommond = [self.todayCommondData subarrayWithRange:NSMakeRange(indexPath.row*3, 3)]; } cell.tapBlock = ^(int index) { weakSelf.vid = index; [weakSelf playItem]; }; return cell; } } return nil; } -(void)playItem{ ZFNormalViewController *vc = [[ZFNormalViewController alloc] init]; vc.vid = self.vid; vc.hidesBottomBarWhenPushed = true; [self.navigationController pushViewController:vc animated:true]; } -(UITableView *)tableView{ if(!_tableView){ _tableView =[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.showsVerticalScrollIndicator = false; _tableView.showsHorizontalScrollIndicator = false; _tableView.backgroundColor = rgba(18, 23, 41, 1); __weak typeof(self) weakSelf = self; _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ weakSelf.tb1footpage += 1; [weakSelf todayReCommond]; }]; // _tableView.backgroundColor = [UIColor redColor]; if (@available(iOS 11, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; _tableView.estimatedRowHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.estimatedSectionHeaderHeight = 0; UIWindow *keyWindow = UIApplication.sharedApplication.keyWindow; UIEdgeInsets safeAreaInsets = keyWindow.safeAreaInsets; CGFloat topSafeArea = safeAreaInsets.top; _tableView.contentInset = UIEdgeInsetsMake(iphoneX ? -topSafeArea : -35 , 0, BAR_HEIGHT, 0); } #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000 // only Xcode 13+ needs and can compile this if (@available(iOS 15.0, *)) { _tableView.sectionHeaderTopPadding = 0; } #endif } return _tableView; } -(SearchCommonBarView *)homeSearchBar{ if(!_homeSearchBar){ _homeSearchBar = [[SearchCommonBarView alloc] initWithFrame:CGRectZero]; _homeSearchBar.backgroundColor = [UIColor clearColor]; __weak typeof(self) weakSelf = self; _homeSearchBar.searchAction = ^(NSString * _Nonnull tit) { [weakSelf homeSearchWithKeyword:tit]; }; } return _homeSearchBar; } -(HomeCommonBarView *)customerbar{ if(!_customerbar){ CGFloat h=0; if(@available(iOS 13.0, *)){ UIStatusBarManager *sc= [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager; CGRect f=sc.statusBarFrame; h=f.size.height; }else{ h=[UIApplication sharedApplication].statusBarFrame.size.height; } if(h>=40){ h=30; } _customerbar =[[HomeCommonBarView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 72-20+h)]; // _customerbar.bgView.backgroundColor = rgba(18, 23, 41, 1); // _customerbar.label.text=@""; // _customerbar.bgView.alpha = 0; // _customerbar.bgView.backgroundColor=[UIColor whiteColor]; // _customerbar.label.text=@"管理图层排序"; // _customerbar.label.textColor = rgba(255, 255, 255, 1); // [_customerbar.leftButton setImage:[UIImage imageNamed:@"his_back_w"] forState:UIControlStateNormal]; // [_customerbar.rightButton setImage:[UIImage imageNamed:@"his_share_w"] forState:UIControlStateNormal]; // [_customerbar.rightButton setTitleColor:RGBACOLOR(141, 146, 168, 1) forState:UIControlStateNormal]; // _customerbar.rightButton.titleLabel.font =[UIFont systemFontOfSize:14]; // [_customerbar.leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; // [_customerbar.rightButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside]; // [_customerbar.rightButton mas_remakeConstraints:^(MASConstraintMaker *make) { // make.centerY.equalTo(_customerbar.leftButton); // make.right.equalTo(_customerbar).offset(-15); // make.size.mas_equalTo(CGSizeMake(23, 23)); // }]; } return _customerbar; } @end #pragma mark -- homecell1 @interface homecell1() @property(nonatomic,strong)UIScrollView *scrollView; @property(nonatomic,strong)NSTimer *mytimer; @end @implementation homecell1 + (instancetype)cellWithTableView:(UITableView *)tableView{ static NSString *identifier = @"homecell1"; homecell1 *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[homecell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } return cell; } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // [self creatView]; } return self; } -(void)setBanner:(NSArray *)banner{ _banner = banner; _imageCount = (int)banner.count; [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; [self.scrollView removeFromSuperview]; [self creatView]; if(self.mytimer){ [self.mytimer invalidate]; self.mytimer = nil; } if(banner.count > 1){ __weak typeof(self) weakSelf = self; self.mytimer = [NSTimer scheduledTimerWithTimeInterval:5.0 repeats:YES block:^(NSTimer * _Nonnull timer) { [weakSelf changeContentAct]; }]; [[NSRunLoop currentRunLoop ] addTimer:self.mytimer forMode:NSRunLoopCommonModes]; } } -(void)changeContentAct{ CGPoint x= self.scrollView.contentOffset; if(x.x >= kScreenWidth*(self.imageCount-1)){ [self.scrollView setContentOffset:CGPointMake(0, x.y)]; }else{ [UIView animateWithDuration:0.3 animations:^{ [self.scrollView setContentOffset:CGPointMake(x.x+kScreenWidth, x.y)]; }]; } } -(void)toplayAct:(UITapGestureRecognizer *)gest{ int x = (int)gest.view.tag - 1000; __weak typeof (self) weakSelf = self; if(self.tapBlock){ self.tapBlock([weakSelf.banner[x][@"vid"] intValue]); } } -(void)creatView{ self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = [UIColor clearColor]; [self.contentView addSubview:self.scrollView]; [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { //375 340 make.size.mas_equalTo(CGSizeMake(kScreenWidth, kScreenWidth*340/375)); make.top.left.equalTo(self.contentView); }]; self.scrollView.contentSize = CGSizeMake(kScreenWidth*self.imageCount, 0); for(int i=0;i