// // MainViewController.m // BuWanVideo2.0 // // Created by weikou on 16/7/29. // Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved. // #import "MainViewController.h" #import "recommendView.h" #import "liveOnLineView.h" #import "subregionView.h" #import "discoverView.h" #import "attentionView.h" #import "searchViewController.h" #import "LookNoteController.h" #import "UIButton+YYWebImage.h" #import "SlideMenuController.h" //#import "XGPush.h" #import "XYRDetailViewController.h" #import "XYRVideoInfoModel.h" #import "AppDelegate.h" #import "MymessageContrlloer.h" #import "OnlySpecialController.h" @interface MainViewController (){ recommendView *_recommend; liveOnLineView *_liveOnLine; subregionView *_subregion; discoverView *_discover; attentionView *_attention; } @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) HMSegmentedControl *segmentedControl; @property (nonatomic, assign) BOOL userOnLine;//用户是否登录 @property (nonatomic, strong) UIView *Point; //提示红点 @property (nonatomic, strong) NSMutableArray *dataClass;//分区数据 @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; _dataClass = @[].mutableCopy; //加载导航栏 [self loadNavigationbar]; //检查当前用户是否登录 [self whetherLogin]; //添加主视图的segment [self creatSegmentView]; //添加通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushMessageVC:) name:@"pushMessageNotification" object:nil]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.navigationController.navigationBarHidden = NO; self.view.backgroundColor = kGlobalBackgroundColor; //[MobClick beginLogPageView:@"主四页"]; //强制竖屏 NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown]; [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"]; NSNumber *PortraitorientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:PortraitorientationUnknown forKey:@"orientation"]; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; //[MobClick endLogPageView:@"主四页"]; } -(BOOL)shouldAutorotate{ return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait ; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return UIInterfaceOrientationPortrait ; } /** * 加载导航栏 */ -(void)loadNavigationbar{ //去掉titlie UIView *tview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0.01, 0.01)]; self.navigationItem.titleView = tview; //观看记录 UIButton *recordBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; [recordBtn setImage:[UIImage imageNamed:@"历史记录"] forState:UIControlStateNormal]; [recordBtn addTarget:self action:@selector(JumpToRecordView) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *recordBarItem=[[UIBarButtonItem alloc] initWithCustomView:recordBtn]; // //下载 // UIButton *downLoadBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; // downLoadBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 4, 0, 4); // [downLoadBtn setImage:[UIImage imageNamed:@"缓存"] forState:UIControlStateNormal]; // [downLoadBtn addTarget:self action:@selector(JumpToDownloadView) forControlEvents:UIControlEventTouchUpInside]; // UIBarButtonItem *downLoadBarItem=[[UIBarButtonItem alloc] initWithCustomView:downLoadBtn]; //搜索 UIButton *SearchBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; [SearchBtn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal]; [SearchBtn addTarget:self action:@selector(JumpToSearchView) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *SearchBarItem=[[UIBarButtonItem alloc] initWithCustomView:SearchBtn]; //空白间距 UILabel *WhiteLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 8, 10)]; WhiteLabel.backgroundColor=[UIColor clearColor]; UIBarButtonItem *WhiteBarItem=[[UIBarButtonItem alloc] initWithCustomView:WhiteLabel]; //添加 观看记录/下载/搜索 到导航栏 self.navigationItem.rightBarButtonItems=[[NSArray alloc] initWithObjects:SearchBarItem,WhiteBarItem,recordBarItem,nil]; } /** * 当前是否登录 */ -(void)whetherLogin{ //调整头像和用户名 [self loadDATA]; //添加一个观察者监听登录和退出登录 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadDATA) name:@"RELOAD_DATA" object:nil]; } /** * 响应登录监听方法的函数 */ -(void)loadDATA{ //重新获取登录状态 _userOnLine= [[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"]; //获取头像和用户名的按钮 //定制显示头像的Button UIButton *iconBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; iconBtn.imageView.contentMode=UIViewContentModeScaleAspectFit; iconBtn.layer.masksToBounds=YES; iconBtn.layer.cornerRadius=22.0; [iconBtn addTarget:self action:@selector(presentLeftMenuViewController) forControlEvents:UIControlEventTouchUpInside]; //定制显示用户名的Button UIButton *nameBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 20)]; nameBtn.backgroundColor=[UIColor clearColor]; nameBtn.titleLabel.font=[UIFont systemFontOfSize:15]; [nameBtn setTintColor:[UIColor whiteColor]]; [nameBtn addTarget:self action:@selector(presentLeftMenuViewController) forControlEvents:UIControlEventTouchUpInside]; nameBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft; if(_userOnLine){//如果当前状态是登录状态,说明用户进行了登录操作 [iconBtn yy_setImageWithURL:[[NSUserDefaults standardUserDefaults] objectForKey:@"iconURL"] forState:UIControlStateNormal placeholder:[UIImage imageNamed:@"默认头像forios11"]]; [nameBtn setTitle:[[NSUserDefaults standardUserDefaults] objectForKey:@"QQUserName"] forState:UIControlStateNormal]; NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"iconURL"]); //设置推送 // [XGPush setAccount:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"]]; }else{//否则用户进行了退出登录的操作 [iconBtn setImage:[UIImage imageNamed:@"默认头像forios11"] forState:UIControlStateNormal]; [nameBtn setTitle:@"立即登录" forState:UIControlStateNormal]; //取消账号推送 // [XGPush setAccount:nil]; } UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:iconBtn]; UIBarButtonItem *nameBarItem=[[UIBarButtonItem alloc] initWithCustomView:nameBtn]; self.navigationItem.leftBarButtonItems=[[NSArray alloc] initWithObjects:iconBarItem,nameBarItem,nil]; } /** * 添加主视图的segment */ -(void)creatSegmentView{ self.segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"推荐 ", @"分区 ", @"发现 ", @"关注 "]]; [self.segmentedControl setFrame:CGRectMake(0, KScreenH - 40, KScreenW, 40)]; self.segmentedControl.selectionIndicatorHeight = 2.0f; self.segmentedControl.backgroundColor = kGlobalMainColor; self.segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName :[UIFont systemFontOfSize:15]}; self.segmentedControl.selectedTitleTextAttributes=@{NSForegroundColorAttributeName : kGlobalYellowColor,NSFontAttributeName :[UIFont systemFontOfSize:15]}; self.segmentedControl.selectionIndicatorColor = kGlobalYellowColor; self.segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe; self.segmentedControl.selectedSegmentIndex = HMSegmentedControlNoSegment; self.segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown; self.segmentedControl.selectedSegmentIndex=0; self.segmentedControl.shouldAnimateUserSelection = YES; __weak typeof(self) weakSelf = self; [self.segmentedControl setIndexChangeBlock:^(NSInteger index) { [weakSelf.scrollView scrollRectToVisible:CGRectMake(KScreenW * index, kNavigationBarH+40, KScreenW, KScreenH-kNavigationBarH-40) animated:YES]; if(index == 3 && _Point){ [weakSelf.Point removeFromSuperview]; [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"HaveAttention"]; } }]; [self.view addSubview:self.segmentedControl]; //配置滑动视图 self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavigationBarH, KScreenW, KScreenH-kNavigationBarH-40)]; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.contentSize = CGSizeMake(KScreenW * 4, KScreenH-kNavigationBarH-40); self.scrollView.delegate = self; [self.scrollView scrollRectToVisible:CGRectMake(0, 0, KScreenW, KScreenH-kNavigationBarH-40) animated:NO]; [self.view addSubview:self.scrollView]; //推荐 _recommend=[[recommendView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH-kNavigationBarH-40)]; [self.scrollView addSubview:_recommend]; //直播 // _liveOnLine=[[liveOnLineView alloc] initWithFrame:CGRectMake(KScreenW, 0, KScreenW, KScreenH-kNavigationBarH-40)]; [self.scrollView addSubview:_liveOnLine]; //分区 _subregion=[[subregionView alloc] initWithFrame:CGRectMake(KScreenW*1, 0, KScreenW, KScreenH-kNavigationBarH-40)]; [self.scrollView addSubview:_subregion]; //发现 _discover=[[discoverView alloc] initWithFrame:CGRectMake(KScreenW*2, 0, KScreenW, KScreenH-kNavigationBarH-40)]; _discover.mycontroller = self; [self.scrollView addSubview:_discover]; //关注 _attention=[[attentionView alloc] initWithFrame:CGRectMake(KScreenW*3, 0, KScreenW, KScreenH-kNavigationBarH-40)]; _attention.temp = self; [self.scrollView addSubview:_attention]; //延迟加载 需要读取segmentedControl 的位置直接加载没有值 if([[NSUserDefaults standardUserDefaults] boolForKey:@"HaveAttention"] && [[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"]){ [self performSelector:@selector(initPont) withObject:nil afterDelay:4]; } //登录后进行是否有新的关注提醒 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadScene) name:@"RELOAD_DATA" object:nil]; } -(void)ReloadScene{ if([[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"] && [[NSUserDefaults standardUserDefaults] boolForKey:@"HaveAttention"]){ //用户已经登录 [self initPont]; } } -(void)initPont{ _Point = [UIView new]; _Point.backgroundColor = [UIColor redColor]; _Point.frame = CGRectMake(self.segmentedControl.ThereRect.origin.x + self.segmentedControl.ThereRect.size.width,(self.segmentedControl.frame.size.height - 8) / 2, 6, 6); _Point.layer.cornerRadius = 3; _Point.layer.masksToBounds =YES; [self.segmentedControl addSubview:_Point]; } //展开侧边栏 -(void)presentLeftMenuViewController{ [self.slideMenuController openLeft]; } /** * 跳转到历史记录 */ -(void)JumpToRecordView{ LookNoteController *LookNoteLookNoteVC=[[LookNoteController alloc] init]; [LookNoteLookNoteVC setHidesBottomBarWhenPushed:YES]; [self.navigationController pushViewController:LookNoteLookNoteVC animated:YES]; } /** * 跳转到搜索 */ -(void)JumpToSearchView{ searchViewController *searchVC=[[searchViewController alloc] init]; [self.navigationController pushViewController:searchVC animated:YES]; } - (void)pushMessageVC:(NSNotification *)sender{ NSDictionary *dic=sender.userInfo; if([[dic objectForKey:@"PushType"] isEqualToString:@"pushVideoUpdateByAccount"]){//跳转到详细界面 NSString *userInfo=[dic objectForKey:@"Params"]; NSDictionary *Params = [self dictionaryWithJsonString:userInfo]; XYRDetailViewController *play = [[XYRDetailViewController alloc]init]; [YTHsharedManger startManger].preController = self; play.modalPresentationStyle = 0; play.Model = [XYRVideoInfoModel yy_modelWithDictionary:Params]; [self presentViewController:play animated:YES completion:^{ }]; }else if([[dic objectForKey:@"PushType"] isEqualToString:@"pushSystemMessage"]){//跳转到系统消息 MymessageContrlloer *message = [MymessageContrlloer new]; [self.navigationController pushViewController:message animated:YES]; }else if([[dic objectForKey:@"PushType"] isEqualToString:@"pushCommentMessage"]){//跳转到我的评论 MymessageContrlloer *message = [MymessageContrlloer new]; [self.navigationController pushViewController:message animated:YES]; }else if([[dic objectForKey:@"PushType"] isEqualToString:@"pushSpecialMessage"]){//跳转到专辑详情页面 OnlySpecialController *OnlySpecial =[OnlySpecialController new]; OnlySpecial.Id=[[dic objectForKey:@"Id"] intValue]; [self.navigationController pushViewController:OnlySpecial animated:YES]; } } #pragma mark 把json格式的字符串转换为字典格式 - (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return nil; } NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; if(err) { NSLog(@"json解析失败:%@",err); return nil; } return dic; } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pageWidth =KScreenW; NSInteger page = scrollView.contentOffset.x / pageWidth; if(page == 4 && _Point){ [_Point removeFromSuperview]; [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"HaveAttention"]; } [self.segmentedControl setSelectedSegmentIndex:page animated:YES]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ if(scrollView.contentOffset.x < -50){ [self presentLeftMenuViewController]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end