// // TPMainPageViewController.m // ThePaperDemo // // Created by Scar on 14-9-2. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "AppDelegate.h" #import "TPMainPageViewController.h" #import "StyledPageControl.h" #import "visualDataMethod.h" #import #import "channalTopicController.h" #import "TestViewController.h" #import "channelMenuView.h" #import "CoreAnimationEffect.h" #import "detailContentHomeController.h" #import "TPSettingController.h" #import "followManagerController.h" #import "collectManagerController.h" #import "askManagerController.h" #import "channalAnswerController.h" #import "askDetailController.h" #import "loginHomeController.h" #import "userInfoController.h" #import "messageController.h" #import "frontPageBannerCell.h" #import "myTopicCenterController.h" #import "TPMainHelpController.h" #import "channalTopicController.h" #import "Reachability.h" #import "pointsMallController.h" @interface TPMainPageViewController () { BOOL isBannerAdHaveShow; } @property(nonatomic, strong)NSArray *viewControllers; @property(nonatomic, strong)NSMutableArray *pageViewControllers; @property(nonatomic, strong)NSArray *mainTitles; //主标题 @property(nonatomic, strong)NSArray *assitTitles; //副标题 @property(nonatomic, strong)NSMutableArray *mainTitleLabels; //标题标签 @property(nonatomic, strong)NSMutableArray *assistTitleLabels; //副标题标签 @property(nonatomic, assign)CGPoint contentOffset; @property(nonatomic, strong)StyledPageControl *pageControl; //页码指示器 @property(nonatomic, strong)UIButton *addBtn; //添加订阅按钮 @property(nonatomic, strong)UIButton *listBtn; //调整导航按钮 @property(nonatomic, assign)BOOL isOpenList; @property(nonatomic, strong)NSMutableArray *adBannerViewList; @property(nonatomic, strong)TPMainHelpController *mainHelpVC; @property(nonatomic, strong)UILabel *noDataLabel; @end @implementation TPMainPageViewController @synthesize pageView; @synthesize viewControllers; @synthesize pageViewControllers; @synthesize mainTitles = _mainTitles; @synthesize assitTitles = _assitTitles; @synthesize contentOffset = _contentOffset; @synthesize isOpenList; @synthesize adBannerViewList; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization isBannerAdHaveShow = NO; } return self; } #pragma mark - some property of page - (void)setMainTitles:(NSArray *)titles { _mainTitles = titles; [self addMainTitleLabel]; self.pageControl.numberOfPages = self.mainTitleLabels.count; } - (void)setAssitTitles:(NSArray *)titles { _assitTitles = titles; [self addAssistTitleLabels]; } //viewControllers in page view - (void) setViewControllers:(NSArray *)controllers { viewControllers = controllers; } - (NSMutableArray *)mainTitleLabels { if (!_mainTitleLabels) { _mainTitleLabels = [[NSMutableArray alloc] initWithCapacity:0]; } return _mainTitleLabels; } - (NSMutableArray *)assistTitleLabels { if (!_assistTitleLabels) { _assistTitleLabels = [[NSMutableArray alloc] initWithCapacity:0]; } return _assistTitleLabels; } //set content offset - (void)setContentOffset:(CGPoint)offset { _contentOffset = offset; CGFloat xOffset = offset.x; // if (xOffset<0 && self.pageView.currentPageIndex == 0) { // return; // } CGFloat normalWidth = CGRectGetWidth(self.naviBar.bounds); CGFloat leftPadding = CGRectGetWidth(self.naviBar.bounds)-100; CGFloat scale = CGRectGetWidth(self.view.bounds)/leftPadding; [self.mainTitleLabels enumerateObjectsUsingBlock:^(UILabel *titleLabel, NSUInteger idx, BOOL *stop) { if ([titleLabel isKindOfClass:[UILabel class]]) { // frame CGRect titleLabelFrame = titleLabel.frame; CGFloat oriY = 25-16; if (isIOS7) { oriY = 25; } titleLabelFrame.origin.y = oriY; titleLabelFrame.origin.x = 50+idx*leftPadding - xOffset / scale; titleLabel.frame = titleLabelFrame; // alpha CGFloat alpha; if(xOffset < normalWidth * idx) { alpha = (xOffset/ normalWidth - idx)*2 + 1; }else{ alpha = 1 - ((xOffset - normalWidth * idx) / normalWidth)*2; } titleLabel.alpha = alpha; } }]; [self.assistTitleLabels enumerateObjectsUsingBlock:^(UILabel *titleLabel, NSUInteger idx, BOOL *stop) { if ([titleLabel isKindOfClass:[UILabel class]]) { // frame CGRect titleLabelFrame = titleLabel.frame; titleLabelFrame.origin.x = 50+idx*leftPadding - xOffset / scale; titleLabel.frame = titleLabelFrame; // alpha CGFloat alpha; if(xOffset < normalWidth * idx) { alpha = (xOffset/ normalWidth - idx)*2 + 1; }else{ alpha = 1 - ((xOffset - normalWidth * idx) / normalWidth)*2; } titleLabel.alpha = alpha; } }]; } - (StyledPageControl *)pageControl { if (!_pageControl) { if (isIOS7) { _pageControl = [[StyledPageControl alloc] initWithFrame:(CGRect){CGRectGetWidth(self.naviBar.bounds)/2-50, 68, 100, 10}]; }else { _pageControl = [[StyledPageControl alloc] initWithFrame:(CGRect){CGRectGetWidth(self.naviBar.bounds)/2-50, 50, 100, 10}]; } _pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; _pageControl.pageControlStyle = PageControlStyleDefault; _pageControl.userInteractionEnabled = NO; _pageControl.hidesForSinglePage = YES; _pageControl.currentPage = self.pageView.currentPageIndex; _pageControl.coreSelectedColor = [UIColor colorWithHexString:@"0x1b84cf"]; _pageControl.coreNormalColor = [UIColor colorWithHexString:@"0xc5c5c5"]; _pageControl.strokeWidth = 5; _pageControl.gapWidth = 4; _pageControl.diameter = 13; } return _pageControl; } - (UIButton*)addBtn { if (!_addBtn) { _addBtn = [UIButton buttonWithType:UIButtonTypeCustom]; if (isIOS7) { _addBtn.frame = CGRectMake(0, 18, 120/2, 120/2); }else { _addBtn.frame = CGRectMake(0, 0, 120/2, 120/2); } if ([[TPUserDefault instance].isNightMode intValue] == 0) { [_addBtn setImage:Image(@"frontPage/add_btn.png") forState:UIControlStateNormal]; [_addBtn setImage:Image(@"frontPage/add_btn_s.png") forState:UIControlStateHighlighted]; }else { [_addBtn setImage:Image(@"frontPage/add_btn_night.png") forState:UIControlStateNormal]; [_addBtn setImage:Image(@"frontPage/add_btn_night_s.png") forState:UIControlStateHighlighted]; } [_addBtn setImageEdgeInsets:UIEdgeInsetsMake(20, 15, 20, 25)]; [_addBtn addTarget:self action:@selector(handleAddBtn:) forControlEvents:UIControlEventTouchUpInside]; } if ([TPUserDefault instance].channldataList.count == 0) { _addBtn.enabled = NO; }else _addBtn.enabled = YES; return _addBtn; } - (UIButton*)listBtn { if (!_listBtn) { _listBtn = [UIButton buttonWithType:UIButtonTypeCustom]; if (isIOS7) { _listBtn.frame = CGRectMake(CGRectGetWidth(self.naviBar.frame)-120/2, 18, 120/2, 120/2); }else { _listBtn.frame = CGRectMake(CGRectGetWidth(self.naviBar.frame)-120/2, 0, 120/2, 120/2); } if ([[TPUserDefault instance].isNightMode intValue] == 0) { [_listBtn setImage:Image(@"frontPage/list_btn.png") forState:UIControlStateNormal]; [_listBtn setImage:Image(@"frontPage/list_btn_s.png") forState:UIControlStateHighlighted]; }else { [_listBtn setImage:Image(@"frontPage/list_btn_night.png") forState:UIControlStateNormal]; [_listBtn setImage:Image(@"frontPage/list_btn_night_s.png") forState:UIControlStateHighlighted]; } // _listBtn.backgroundColor = [UIColor greenColor]; [_listBtn setImageEdgeInsets:UIEdgeInsetsMake(20, 25, 20, 15)]; [_listBtn addTarget:self action:@selector(handleListBtn:) forControlEvents:UIControlEventTouchUpInside]; } return _listBtn; } #pragma mark - load view - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; if ([TPUserDefault instance].channldataList.count == 0) { NSDictionary *dic = @{@"type":@"1"}; [Remote doJsonAction:1 requestUrl:allNodeListURL parameter:dic delegate:self]; dic = @{@"type":@"4"}; [Remote doJsonAction:2 requestUrl:allNodeListURL parameter:dic delegate:self]; }else { [self setAdBannerViews]; [self addSubViews]; } AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; YRSideViewController *sideController=[delegate sideViewController]; sideController.delegate = self; delegate.welcomeController = nil; __weak typeof(self) Self = self; [sideController setRootViewMoveBlock:^(UIView *rootView, CGRect orginFrame, CGFloat xoffset) { //使用简单的平移动画 if (xoffset ==0) { if (Self.pageView.currentPageIndex != 0 && !isOpenList) { AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; YRSideViewController *sideVC=[delegate sideViewController]; sideVC.needSwipeShowMenu = NO; } } }]; isOpenList = NO; [sideController setNeedsStatusBarAppearanceUpdate]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; //notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToLogin:) name:PUSHTOLOGIN object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToUserInfo:) name:PUSHTOUSERINFO object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToSettings:) name:PUSHTOSETTINGS object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToFollow:) name:PUSHTOFOLLOW object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToCollect:) name:PUSHTOCOLLECT object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToAskManager:) name:PUSHTOASK object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToMessage:) name:PUSHTOMESSAGE object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToMyTopic:) name:PUSHMYTOPICCENTER object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToHaiBei:) name:PUSHTOHAIBEI object:nil]; } #pragma mark - backBtnClick -(void)disHelp{ [UIView animateWithDuration:0.3 animations:^{ [self.mainHelpVC.view removeFromSuperview]; }completion:^(BOOL finished) { self.mainHelpVC = nil; needGestureOfSideController(YES); }]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:NO]; if (self.pageView.currentPageIndex == 0) { needGestureOfSideController(YES); }else { needGestureOfSideController(NO); } [TPUserDefault instance].isOffline = NO; } - (void)addSubViews { [self setPageViews]; [self.naviBar addSubview:self.addBtn]; [self.naviBar addSubview:self.listBtn]; if (isIOS7) { self.listBtn.frame = CGRectMake(CGRectGetWidth(self.naviBar.frame)-120/2, 18, 120/2, 120/2); }else { self.listBtn.frame = CGRectMake(CGRectGetWidth(self.naviBar.frame)-120/2, 0, 120/2, 120/2); } [self followScrollView:self.pageView]; [self.mainTitleLabels enumerateObjectsUsingBlock:^(UILabel* obj, NSUInteger idx, BOOL *stop) { [self.naviBar bringSubviewToFront:obj]; }]; if ([[TPUserDefault instance].helpArray[@"front"] intValue] == 0) { needGestureOfSideController(NO); NSMutableDictionary *array = [NSMutableDictionary dictionaryWithDictionary:[TPUserDefault instance].helpArray]; [array setObject:@"1" forKey:@"front"]; [TPUserDefault instance].helpArray = array; self.mainHelpVC = [TPMainHelpController new]; self.mainHelpVC.delegate =self; [self.view addSubview:self.mainHelpVC.view]; [self.mainHelpVC.view makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.top.equalTo(self.view.top); make.bottom.equalTo(self.view.bottom); }]; } } - (void)setPageViews { self.pageView = [[ATPagingView alloc] initWithFrame:CGRectMake(0, self.barHeight, self.view.frame.size.width, self.view.frame.size.height-self.barHeight)]; self.pageView.gapBetweenPages = 0; self.pageView.recyclingEnabled = NO; self.pageView.backgroundColor = [UIColor clearColor]; self.pageView.delegate = self; self.pageView.scrollView.scrollEnabled = YES; self.pageView.scrollView.bounces = NO; [self.view addSubview:self.pageView]; NSMutableArray *userSourceArray = [TPUserDefault instance].channldataList; if (userSourceArray.count > 0) { NSMutableArray *main = [NSMutableArray array]; NSMutableArray *assist = [NSMutableArray array]; NSMutableArray *vcs = [NSMutableArray array]; [userSourceArray enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) { [main addObject:dic[@"name"]?dic[@"name"]:@""]; [assist addObject:dic[@"enname"]?dic[@"enname"]:@""]; if ([dic.allKeys containsObject:@"controller"]) { [vcs addObject:dic[@"controller"]?dic[@"controller"]:@""]; } }]; self.mainTitles = [NSArray arrayWithArray:main]; self.assitTitles = [NSArray arrayWithArray:assist]; self.viewControllers = [NSArray arrayWithArray:vcs]; } [viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if (nil == self.pageViewControllers) { self.pageViewControllers = [NSMutableArray array]; } if ([obj isKindOfClass:[UIViewController class]]) { [self.pageViewControllers addObject:obj]; } else { [self.pageViewControllers addObject:[NSNull null]]; } }]; UIView *view = self.pageView; [self.pageView removeFromSuperview]; view.frame = CGRectMake(0, self.barHeight, self.view.frame.size.width, self.view.frame.size.height-self.barHeight); [self.view addSubview:view]; [self.pageView reloadData]; [self.naviBar addSubview:self.pageControl]; [self.pageView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.top.equalTo(self.naviBar.bottom); make.bottom.equalTo(self.view.bottom); }]; } - (void)addMainTitleLabel { if (!self.mainTitles.count) { return; } [self.mainTitleLabels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) { label.hidden = YES; }]; [self.mainTitles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL *stop) { UILabel *titleLabel = (UILabel *)[self.view viewWithTag:kXHLabelBaseTag + idx]; CGRect titleLabelFrame = CGRectMake(0, 0, 0, 0); if (isIOS7) { titleLabelFrame = CGRectMake(50+(idx * 250), 25, CGRectGetWidth(self.naviBar.bounds)-100, 20); }else { titleLabelFrame = CGRectMake(50+(idx * 250), 25-16, CGRectGetWidth(self.naviBar.bounds)-100, 20); } if (!titleLabel) { titleLabel = [[UILabel alloc] init]; [self.view addSubview:titleLabel]; [self.mainTitleLabels addObject:titleLabel]; titleLabel.tag = kXHLabelBaseTag+idx; } titleLabel.hidden = NO; titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; titleLabel.font = appFont(TEXT_ONE_PLUS_LEVELSIZE, NO); titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.textColor = [UIColor colorWithHexString:TextBlack]; titleLabel.backgroundColor = [UIColor clearColor]; titleLabel.text = title; titleLabel.frame = titleLabelFrame; if (self.pageView.currentPageIndex == idx) { titleLabel.alpha = 1.0; } else { titleLabel.alpha = 0.0; } // CGFloat leftPadding = CGRectGetWidth(self.naviBar.bounds)-100; // // [titleLabel makeConstraints:^(MASConstraintMaker *make) { // make.left.equalTo(self.naviBar.left).offset(50+idx*leftPadding); // if (isIOS7) { // make.top.equalTo(self.naviBar.top).offset(25); // }else { // make.top.equalTo(self.naviBar.top).offset(25-16); // } // make.width.equalTo(@(leftPadding)); // make.height.equalTo(@20); // }]; }]; } - (UILabel*)noDataLabel { if (!_noDataLabel) { _noDataLabel = [UILabel new]; _noDataLabel.font = appFont(TEXT_ONE_LEVELSIZE, NO); _noDataLabel.textColor = [UIColor colorWithHexString:TextBlack]; _noDataLabel.backgroundColor = [UIColor clearColor]; _noDataLabel.text = @"澎湃"; _noDataLabel.textAlignment = NSTextAlignmentCenter; } return _noDataLabel; } - (void)addAssistTitleLabels { if (!self.assitTitles.count) { return; } [self.assistTitleLabels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) { label.hidden = YES; }]; [self.assitTitles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL *stop) { CGRect titleLabelFrame = CGRectMake(0, 0, 0, 0); if (isIOS7) { titleLabelFrame = CGRectMake(50+(idx * 250), 45, CGRectGetWidth(self.naviBar.bounds)-100, 20); }else { titleLabelFrame = CGRectMake(50+(idx * 250), 45-16, CGRectGetWidth(self.naviBar.bounds)-100, 20); } UILabel *titleLabel = (UILabel *)[self.naviBar viewWithTag:kXHLabelAssistTag + idx]; if (!titleLabel) { titleLabel = [[UILabel alloc] init]; [self.naviBar addSubview:titleLabel]; [self.assistTitleLabels addObject:titleLabel]; titleLabel.tag = kXHLabelAssistTag+idx; } titleLabel.hidden = NO; titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; titleLabel.font = appFont(TEXT_SEVEN_LEVELSIZE, NO); titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.textColor = [UIColor colorWithHexString:TextBlack]; titleLabel.backgroundColor = [UIColor clearColor]; titleLabel.text = title; titleLabel.frame = titleLabelFrame; if (self.pageView.currentPageIndex == idx) { titleLabel.alpha = 1.0; } else { titleLabel.alpha = 0.0; } }]; } #pragma mark - ATPagingViewDelegate methods - (NSInteger)numberOfPagesInPagingView:(ATPagingView *)pagingView { return self.viewControllers.count; } - (UIView *)viewForPageInPagingView:(ATPagingView *)pagingView atIndex:(NSInteger)index { if (self.pageViewControllers[index] == [NSNull null]) { NSString *className = self.viewControllers[index]; Class class = NSClassFromString(className); UIViewController *vc = [[class alloc] init]; self.pageViewControllers[index] = vc; [self addChildViewController:vc]; // if ([vc isKindOfClass:[channalAnswerController class]]) { // [((channalAnswerController*)vc) goToAskDetailPage:^(id commentObj) { // askDetailController *vc = askDetailController.new; // vc.commentBO = commentObj; // [self.navigationController pushViewController:vc animated:YES]; // }]; // } } if([self.pageViewControllers[index] isKindOfClass:[channelContentListController class]]){ return ((channelContentListController*)self.pageViewControllers[index]).view; }else{ return ((channalTopicController*)self.pageViewControllers[index]).view; } } - (void)callBackFromClickCard:(UIViewController*)vc { } - (void)actionForNavigationController:(NSInteger)index pageViewControllers:(NSMutableArray*)pageArray{ } - (void)currentPageDidChangeInPagingView:(ATPagingView *)pagingView { } - (void)pagesDidChangeInPagingView:(ATPagingView *)view { self.currentIndex = self.pageView.currentPageIndex; if (view.currentPageIndex == 0) { needGestureOfSideController(YES); }else { needGestureOfSideController(NO); } self.pageControl.currentPage = view.currentPageIndex; [TPUserDefault instance].currentPageIndex = view.currentPageIndex; if (self.pageViewControllers[self.pageView.currentPageIndex] && [self.pageViewControllers[self.pageView.currentPageIndex] isKindOfClass:[channelContentListController class]]) { channelContentListController *listVC = (channelContentListController*)self.pageViewControllers[self.pageView.currentPageIndex]; [listVC pageChangeBegin:self.pageView.currentPageIndex]; NSString *controllerName = self.viewControllers[self.pageView.currentPageIndex]; if ([controllerName isEqualToString:@"channalPoliticsController"]) { [MobClick event:@"1"]; }else if ([controllerName isEqualToString:@"channalEconomyController"]) { [MobClick event:@"2"]; }else if ([controllerName isEqualToString:@"channalInsightsController"]) { [MobClick event:@"3"]; }else if ([controllerName isEqualToString:@"channalLifeController"]) { [MobClick event:@"4"]; }else if ([controllerName isEqualToString:@"channalMyPaperController"]) { [MobClick event:@"6"]; } }else{ [MobClick event:@"59"]; channalTopicController *topicListVC = (channalTopicController*)self.pageViewControllers[self.pageView.currentPageIndex]; [topicListVC pageChangeBegin:self.pageView.currentPageIndex]; } if (!view.scrollView.isDragging && view.currentPageIndex != 0) { frontPageBannerCell *bannerView = [self.adBannerViewList objectAtIndex:view.currentPageIndex]; if (!bannerView.isHaveShow && bannerView.isReady && !isBannerAdHaveShow) { [self showBannerView:bannerView index:view.currentPageIndex]; } } /** * bug:5669( 浮层广告:有两个症状) */ } - (void)pagingViewWillBeginMoving:(ATPagingView *)pagingView { [self disableFollowScroll]; [self resetNaviBar]; } - (void)pagingViewDidEndMoving:(ATPagingView *)pagingView { [self enableFollowScroll]; if (pageView.currentPageIndex != 0) { frontPageBannerCell *bannerView = [self.adBannerViewList objectAtIndex:pageView.currentPageIndex]; if (!bannerView.isHaveShow && bannerView.isReady && !isBannerAdHaveShow) { [self showBannerView:bannerView index:pageView.currentPageIndex]; } } } - (void)pagingViewdDidScroll:(UIScrollView *)scrollerView { self.contentOffset = scrollerView.contentOffset; } #pragma mark - button handle - (void)handleAddBtn:(UIButton*)btn { frontPageBannerCell *banner = self.adBannerViewList[self.currentIndex]; if (banner.showing) { [self dismissAd:self.currentIndex bannerView:banner]; } AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; YRSideViewController *sideController=[delegate sideViewController]; [sideController showLeftViewController:true]; } - (channelMenuView*)menuView { if (!_menuView) { self.menuView = [[channelMenuView alloc]initWithFrame:self.pageView.bounds]; __weak typeof(self) Self = self; [self.menuView changeStauseBarStyle:^{ AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; YRSideViewController *sideController=[delegate sideViewController]; [sideController setNeedsStatusBarAppearanceUpdate]; }]; [self.menuView changeFont:^{ [Self.mainTitleLabels enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[UILabel class]]) { UILabel *label = obj; label.font = appFont(20, NO); } }]; [Self.assistTitleLabels enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[UILabel class]]) { UILabel *label = obj; label.font = appFont(9, NO); } }]; }]; [self.menuView setCloseCompletionHandler:^{ Self.lineView.hidden = NO; Self.isOpenList = NO; if (Self.pageView.currentPageIndex == 0) { needGestureOfSideController(YES); }else needGestureOfSideController(NO); //更新首页页面数据 NSMutableArray *mainArray = [NSMutableArray array]; NSMutableArray *assistArray = [NSMutableArray array]; if ([TPUserDefault instance].channldataList.count > 0) { [[TPUserDefault instance].channldataList enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) { [mainArray addObject:dic[@"name"]]; [assistArray addObject:dic[@"enname"]]; }]; } NSString *oriText = Self.mainTitles[Self.pageView.currentPageIndex]; NSString *currentText = mainArray[[TPUserDefault instance].currentPageIndex]; static CGFloat duration; if ([oriText isEqualToString:currentText]) { duration = 0; }else { duration = 0.3; } // dispatch_async(dispatch_get_main_queue(), ^{ UILabel *mainLabel = Self.mainTitleLabels[Self.pageView.currentPageIndex]; UILabel *assistLabel = Self.assistTitleLabels[Self.pageView.currentPageIndex]; [UIView animateWithDuration:duration animations:^{ if (duration > 0) { mainLabel.alpha = 0; assistLabel.alpha = 0; }else { mainLabel.alpha = 1; assistLabel.alpha = 1; } } completion:^(BOOL finished) { if (duration > 0) { mainLabel.hidden = YES; assistLabel.alpha = YES; }else { mainLabel.hidden = NO; assistLabel.alpha = NO; } //更新首页页面数据 if ([TPUserDefault instance].channldataList.count > 0) { NSMutableArray *userSourceArray = [TPUserDefault instance].channldataList; if (userSourceArray.count > 0) { NSMutableArray *vcs = [NSMutableArray array]; [userSourceArray enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) { [vcs addObject:dic[@"controller"]]; }]; Self.viewControllers = [NSArray arrayWithArray:vcs]; } NSMutableArray *tempPageArray = [NSMutableArray arrayWithArray:Self.pageViewControllers]; [Self.pageViewControllers removeAllObjects]; [[TPUserDefault instance].channldataList enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) { NSString *className = dic[@"controller"]; Class class = NSClassFromString(className); BOOL isAdd = NO; for (NSObject *obj in tempPageArray) { if ([obj isKindOfClass:class]) { [Self.pageViewControllers enumerateObjectsUsingBlock:^(id objx, NSUInteger index, BOOL *stopx) { if ([objx isKindOfClass:class]) { [Self.pageViewControllers removeObject:objx]; } }]; [Self.pageViewControllers addObject:obj]; if ([obj isKindOfClass:[channelContentListController class]]) { channelContentListController *channelListVC = (channelContentListController*)obj; [channelListVC reloadWithFontChange]; }else if ([obj isKindOfClass:[channalTopicController class]]) { channalTopicController *channelListVC = (channalTopicController*)obj; [channelListVC reloadWithFontChange]; /** * bug:5173(切换字体,卡片下方挂的追问和问吧没有变字体,问吧列表没有变字体) */ } isAdd = YES; } } if (!isAdd) { [Self.pageViewControllers addObject:[NSNull null]]; } }]; BOOL isPushRight = NO; if ([TPUserDefault instance].currentPageIndex > Self.pageView.currentPageIndex) { isPushRight = YES; } Self.pageView.currentPageIndex = [TPUserDefault instance].currentPageIndex; if (duration > 0) { if (isPushRight) { [CoreAnimationEffect animationPushRight:Self.pageView]; }else [CoreAnimationEffect animationPushLeft:Self.pageView]; } Self.mainTitles = [NSArray arrayWithArray:mainArray]; Self.assitTitles = [NSArray arrayWithArray:assistArray]; [Self.pageView reloadData]; Self.contentOffset = Self.pageView.scrollView.contentOffset; UILabel *mainLabel = Self.mainTitleLabels[Self.pageView.currentPageIndex]; UILabel *assistLabel = Self.assistTitleLabels[Self.pageView.currentPageIndex]; if (duration > 0) { mainLabel.alpha = 0; assistLabel.alpha = 0; }else { mainLabel.alpha = 1; assistLabel.alpha = 1; } mainLabel.hidden = NO; assistLabel.hidden = NO; [UIView animateWithDuration:duration animations:^{ mainLabel.alpha = 1; assistLabel.alpha = 1; } completion:^(BOOL finished) { [Self.menuView removeFromSuperview]; // Self.menuView = nil; [Self enableFollowScroll]; }]; } }]; // }); }]; } return _menuView; } - (void)handleListBtn:(UIButton*)btn { self.menuView.frame = self.pageView.bounds; frontPageBannerCell *banner = self.adBannerViewList[self.currentIndex]; if (banner.showing) { [self dismissAd:self.currentIndex bannerView:banner]; } if (!isOpenList) { [self disableFollowScroll]; self.lineView.hidden =YES; [self.pageView addSubview:self.menuView]; if (isIOS7) { self.menuView.snapView = customBlurSnapshotFromView(self.pageView); } [self removeBudgeViewInRightButton]; [self.menuView showMenuInView:self.pageView naviHeight:self.barHeight]; // self.pageView.userInteractionEnabled = NO; isOpenList = YES; needGestureOfSideController(YES); [MobClick event:@"14"]; }else { [self enableFollowScroll]; if (self.menuView) { [self.menuView closeWithCompletion:^{ }]; } } } //push to some view form channel center - (void)pushToLogin:(NSNotification*)notification { loginHomeController *loginVC = [[loginHomeController alloc]init]; [self.navigationController pushViewController:loginVC animated:YES]; } - (void)pushToUserInfo:(NSNotification*)notification { userInfoController *vc = [[userInfoController alloc]init]; [self.navigationController pushViewController:vc animated:YES]; } - (void)pushToHaiBei:(NSNotification*)notification { if ([TPUserDefault instance].userBO) { pointsMallController *pointsMallVC = [pointsMallController new]; [self.navigationController pushViewController:pointsMallVC animated:YES]; } else [self pushToLogin:notification]; } - (void)pushToSettings:(NSNotification*)notification { // [self.navigationController pushViewController:[TestViewController new] animated:YES]; TPSettingController *vc = TPSettingController.new; [self.navigationController pushViewController:vc animated:YES]; } - (void)pushToFollow:(NSNotification*)notification { if ([TPUserDefault instance].userBO) { followManagerController *vc = followManagerController.new; [self.navigationController pushViewController:vc animated:YES]; }else{ [self pushToLogin:notification]; } } - (void)pushToCollect:(NSNotification*)notification { if ([TPUserDefault instance].userBO) { collectManagerController *vc = collectManagerController.new; [self.navigationController pushViewController:vc animated:YES]; } else if ([TPUserDefault instance].localCollectArray.count){ collectManagerController *vc = collectManagerController.new; [self.navigationController pushViewController:vc animated:YES]; } else [self pushToLogin:notification]; } - (void)pushToAskManager:(NSNotification*)notification { askManagerController *vc = askManagerController.new; [self.navigationController pushViewController:vc animated:YES]; } - (void)pushToMessage:(NSNotification*)notification { if ([TPUserDefault instance].userBO) { messageController *vc = messageController.new; vc.delegate = self; [vc disablePanGesture]; [self.navigationController pushViewController:vc animated:YES]; }else{ [self pushToLogin:notification]; } } - (void)needrefreshNightMode:(NSNotification*)notification { self.overlay.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.naviBar.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; if ([[TPUserDefault instance].isNightMode intValue]== 1) { [self.addBtn setImage:Image(@"frontPage/add_btn_night.png") forState:UIControlStateNormal]; [self.listBtn setImage:Image(@"frontPage/list_btn_night.png") forState:UIControlStateNormal]; [self.addBtn setImage:Image(@"frontPage/add_btn_night_s.png") forState:UIControlStateHighlighted]; [self.listBtn setImage:Image(@"frontPage/list_btn_night_s.png") forState:UIControlStateHighlighted]; }else { [self.addBtn setImage:Image(@"frontPage/add_btn.png") forState:UIControlStateNormal]; [self.listBtn setImage:Image(@"frontPage/list_btn.png") forState:UIControlStateNormal]; [self.addBtn setImage:Image(@"frontPage/add_btn_s.png") forState:UIControlStateHighlighted]; [self.listBtn setImage:Image(@"frontPage/list_btn_s.png") forState:UIControlStateHighlighted]; } [self.mainTitleLabels enumerateObjectsUsingBlock:^(UILabel* obj, NSUInteger idx, BOOL *stop) { obj.textColor = [UIColor colorWithHexString:TextBlack]; }]; [self.assistTitleLabels enumerateObjectsUsingBlock:^(UILabel* obj, NSUInteger idx, BOOL *stop) { obj.textColor = [UIColor colorWithHexString:TextBlack]; }]; } -(void) pushToMyTopic:(NSNotification*)notification { if ([TPUserDefault instance].userBO) { myTopicCenterController *vc = myTopicCenterController.new; vc.delegate = self; [self.navigationController pushViewController:vc animated:YES]; }else{ [self pushToLogin:notification]; } } #pragma mark #pragma mark - remote call back - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { if (actionTag == 1) { if (responsData && responsData[@"nodeList"]) { NSMutableArray *channalList = [TPUserDefault instance].channldataList; if (channalList.count == 0) { NSArray *nodeArray = responsData[@"nodeList"]; NSMutableArray *tempArray = [NSMutableArray array]; [nodeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSString *nodeID = obj[@"nodeId"]; NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:obj]; [dic setValue:[self getContollerNameWithNodeId:nodeID] forKey:@"controller"]; NSString *color = dic[@"color"]; color = [color substringWithRange:NSMakeRange(1, 6)]; color = [NSString stringWithFormat:@"0x%@",color]; [dic setValue:color forKey:@"color"]; [tempArray addObject:dic]; }]; if (tempArray.count == 7) { [TPUserDefault instance].channldataList = tempArray; [self addSubViews]; } }else { NSMutableArray *tempArray = [NSMutableArray array]; [channalList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:obj]; NSString *nodeID = obj[@"nodeId"]; NSArray *nodeArray = responsData[@"nodeList"]; __block NSMutableDictionary *oriDic = dic; [nodeArray enumerateObjectsUsingBlock:^(id item, NSUInteger index, BOOL *stop1) { if ([nodeID intValue] == [item[@"nodeId"] intValue]) { oriDic = [NSMutableDictionary dictionaryWithDictionary:item]; [oriDic setValue:[self getContollerNameWithNodeId:nodeID] forKey:@"controller"]; NSString *color = oriDic[@"color"]; color = [color substringWithRange:NSMakeRange(1, 6)]; color = [NSString stringWithFormat:@"0x%@",color]; [oriDic setValue:color forKey:@"color"]; *stop1 = YES; } }]; [tempArray addObject:oriDic]; }]; if (tempArray.count == 7) { [TPUserDefault instance].channldataList = tempArray; [self setAdBannerViews]; [self addSubViews]; } } } }else if (actionTag == 2) { if (responsData && responsData[@"nodeList"]) { NSArray *nodeArray = responsData[@"nodeList"]; NSMutableArray *tempArray = [NSMutableArray array]; [nodeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { nodeObjectBO *nodeBO = setJsonDicToDataModel(obj, [nodeObjectBO class]); [tempArray addObject:nodeBO]; }]; [TPUserDefault instance].offlineList = [NSArray arrayWithArray:tempArray]; } } } - (NSString*)getContollerNameWithNodeId:(NSString*)nodeId { if ([nodeId isEqualToString:@"25949"]) { return @"channalCollectController"; }else if ([nodeId isEqualToString:@"25950"]) { return @"channalPoliticsController"; }else if ([nodeId isEqualToString:@"25951"]) { return @"channalEconomyController"; }else if ([nodeId isEqualToString:@"25952"]) { return @"channalInsightsController"; }else if ([nodeId isEqualToString:@"25953"]) { return @"channalLifeController"; }else if ([nodeId isEqualToString:@"-3"]) { return @"channalTopicController"; }else { return @"channalMyPaperController"; } } - (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{ ShowTextMessage(message); if (actionTag == 1 && isBlankString(code) && [message isEqualToString:@"网络连接中断,请检查网络设置!"]) { if ([TPUserDefault instance].channldataList.count == 0) { [self.naviBar addSubview:self.addBtn]; [self.naviBar addSubview:self.listBtn]; self.addBtn.enabled = NO; self.listBtn.enabled = NO; [self.naviBar addSubview:self.noDataLabel]; [self.noDataLabel makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.naviBar.centerX); make.top.equalTo(self.naviBar.top).equalTo(25); make.width.mas_equalTo(CGRectGetWidth(self.naviBar.bounds)-100); make.height.equalTo(@20); }]; } } } #pragma mark - naviBar tap gesture - (void)tapNaviBar:(id)sender { id obj = self.pageViewControllers[self.pageView.currentPageIndex]; if ([obj isKindOfClass:[channelContentListController class]]) { [((channelContentListController*)obj) scrollTableViewToTop]; }else if([obj isKindOfClass:[channalTopicController class]]){ [((channalTopicController*)obj) scrollTableViewToTop]; } } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - ad banner - (void)setAdBannerViews { NSMutableArray *channelList = [TPUserDefault instance].channldataList; self.adBannerViewList = [NSMutableArray array]; [channelList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { frontPageBannerCell *bannerView = [[frontPageBannerCell alloc]initWithFrame:CGRectMake(0, -rect_screen.size.height, rect_screen.size.width, rect_screen.size.height)]; bannerView.naviHeight = self.barHeight; bannerView.adUrl = obj[@"adUrl2"]; bannerView.delegate = self; [self.adBannerViewList addObject:bannerView]; }]; } - (void)showBannerView:(frontPageBannerCell*)bannerView index:(NSInteger)index{ [self.pageView addSubview:bannerView]; if ([UIView respondsToSelector:@selector(animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:)]) { [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:1.0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut animations:^{ CGRect rect = bannerView.frame; rect.origin.y = 0-self.barHeight; bannerView.frame = rect; bannerView.index = index; } completion:^(BOOL finished) { [self disableFollowScroll]; bannerView.showing = YES; needGestureOfSideController(NO); }]; } else { [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut animations:^{ CGRect rect = bannerView.frame; rect.origin.y = 0-self.barHeight; bannerView.frame = rect; bannerView.index = index; } completion:^(BOOL finished) { [self disableFollowScroll]; bannerView.showing = YES; needGestureOfSideController(NO); }]; } } - (void)dismissAd:(NSInteger)index bannerView:(UIView *)bannerView{ [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut animations:^{ CGRect rect = bannerView.frame; rect.origin.y = -rect_screen.size.height; bannerView.frame = rect; } completion:^(BOOL finished) { [self enableFollowScroll]; frontPageBannerCell *banner = (frontPageBannerCell*)bannerView; banner.isHaveShow = YES; isBannerAdHaveShow = YES; banner.showing = NO; [self.adBannerViewList replaceObjectAtIndex:banner.index withObject:banner]; if (self.pageView.currentPageIndex == 0) { needGestureOfSideController(YES); } }]; } - (void)clickAdd:(remotePushBO *)pushBO index:(NSInteger)index bannerView:(UIView *)bannerView { [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut animations:^{ CGRect rect = bannerView.frame; rect.origin.y = -rect_screen.size.height; bannerView.frame = rect; } completion:^(BOOL finished) { [self enableFollowScroll]; frontPageBannerCell *banner = (frontPageBannerCell*)bannerView; banner.isHaveShow = YES; isBannerAdHaveShow = YES; banner.showing = NO; [self.adBannerViewList replaceObjectAtIndex:banner.index withObject:banner]; [TPUserDefault instance].pushBO = pushBO; if (self.pageView.currentPageIndex == 0) { needGestureOfSideController(YES); } }]; } #pragma mark - side controller delegate - (void)leftControllerDidShowed { AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; YRSideViewController *sideController=[delegate sideViewController]; frontPageBannerCell *bannerView = [self.adBannerViewList objectAtIndex:0]; if (!bannerView.isHaveShow && bannerView.isReady && !isBannerAdHaveShow) { [sideController.view addSubview:bannerView]; if ([UIView respondsToSelector:@selector(animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:)]) { [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:1.0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut animations:^{ CGRect rect = bannerView.frame; rect.origin.y = 0; bannerView.frame = rect; bannerView.index = 0; } completion:^(BOOL finished) { [self disableFollowScroll]; bannerView.showing = YES; sideController.needSwipeShowMenu = NO; /** * bug:5422(浮层广告:栏目导航页的浮层广告出现后,左滑操作会将下层的栏目页也一同返回) */ }]; } else { [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut animations:^{ CGRect rect = bannerView.frame; rect.origin.y = 0; bannerView.frame = rect; bannerView.index = 0; } completion:^(BOOL finished) { [self disableFollowScroll]; bannerView.showing = YES; sideController.needSwipeShowMenu = NO; /** * bug:5422(浮层广告:栏目导航页的浮层广告出现后,左滑操作会将下层的栏目页也一同返回) */ }]; } } } //- (UIStatusBarStyle)preferredStatusBarStyle { // if ([[TPUserDefault instance].isNightMode intValue] == 1) { // return UIStatusBarStyleLightContent; // }else { // return UIStatusBarStyleDefault; // } //} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark -- message delegate //- (void)removeAskView:(UIPanGestureRecognizer*)panGesture { // if (panGesture.enabled) { // return; // } //} - (void)removeButtonHandler { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark - msg mark logic - (void)addBudgeViewInRightButton { [self.listBtn addBadgeView]; } - (void)removeBudgeViewInRightButton { [self.listBtn clearBadgeView]; } - (void) reachabilityChanged:(NSNotification *)note { if ([Remote IsEableNetwork]) { if (![TPUserDefault instance].channldataList || [TPUserDefault instance].channldataList.count==0) { [self.addBtn removeFromSuperview]; [self.listBtn removeFromSuperview]; [self.noDataLabel removeFromSuperview]; _noDataLabel = nil; self.addBtn.enabled = YES; self.listBtn.enabled = YES; NSDictionary *dic = @{@"type":@"1"}; [Remote doJsonAction:1 requestUrl:allNodeListURL parameter:dic delegate:self]; dic = @{@"type":@"4"}; [Remote doJsonAction:2 requestUrl:allNodeListURL parameter:dic delegate:self]; } } } @end