|
//
// channelListHomeController.m
// ThePaperHD
//
// Created by scar1900 on 15/2/16.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "channelListHomeController.h"
#import "ATPagingView.h"
#import "channelDetailListController.h"
#import "StyledPageControl.h"
#import "customFlowLayout.h"
#import "hotAskListHomeController.h"
#import "topicListHomeController.h"
@interface channelTitleView : UIView
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *assistLabel;
@property(nonatomic, strong)NSString *title;
@property(nonatomic, strong)NSString *assistTitle;
@property(nonatomic, strong)StyledPageControl *pageControl; //页码指示器
@property(nonatomic, assign)NSInteger pageIndex;
@property(nonatomic, assign)NSInteger numsOfPage;
@end
@implementation channelTitleView
@synthesize title = _title,assistTitle = _assistTitle;
@synthesize pageIndex = _pageIndex;
@synthesize numsOfPage = _numsOfPage;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.titleLabel];
[self addSubview:self.assistLabel];
[self addSubview:self.pageControl];
}
return self;
}
- (void)setTitle:(NSString *)str {
_title = str;
self.titleLabel.text = str;
}
- (void)setAssistTitle:(NSString *)str {
_assistTitle = str;
self.assistLabel.text = str;
}
- (void)setPageIndex:(NSInteger)pageIndex {
_pageIndex = pageIndex;
self.pageControl.currentPage = pageIndex;
}
- (void)setNumsOfPage:(NSInteger)numsOfPage {
_numsOfPage = numsOfPage;
self.pageControl.numberOfPages = numsOfPage;
}
- (UILabel*)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = appFont(38/2, NO);
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.backgroundColor = [UIColor clearColor];
}
return _titleLabel;
}
- (UILabel*)assistLabel {
if (!_assistLabel) {
_assistLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_assistLabel.textColor = [UIColor whiteColor];
_assistLabel.font = appFont(9, NO);
_assistLabel.textAlignment = NSTextAlignmentCenter;
_assistLabel.backgroundColor = [UIColor clearColor];
}
return _assistLabel;
}
- (StyledPageControl *)pageControl {
if (!_pageControl) {
_pageControl = [[StyledPageControl alloc] initWithFrame:CGRectZero];
// _pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_pageControl.pageControlStyle = PageControlStyleDefault;
_pageControl.userInteractionEnabled = NO;
_pageControl.hidesForSinglePage = YES;
_pageControl.coreSelectedColor = [UIColor whiteColor];
_pageControl.coreNormalColor = [UIColor colorWithHexString:@"0xc5c5c5"];
_pageControl.strokeWidth = 5;
_pageControl.gapWidth = 5;
_pageControl.diameter = 13;
_pageControl.backgroundColor = [UIColor clearColor];
}
return _pageControl;
}
- (void)layoutSubviews {
self.titleLabel.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 20);
self.assistLabel.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), CGRectGetWidth(self.bounds), 12);
self.pageControl.frame= CGRectMake(0, CGRectGetMaxY(self.assistLabel.frame), CGRectGetWidth(self.bounds), 10);
}
@end
@interface channelListHomeController ()<UIGestureRecognizerDelegate,UIPageViewControllerDelegate, UIPageViewControllerDataSource>
@property(nonatomic, strong)UIPageViewController *pageViewController;
@property(nonatomic, strong)NSMutableArray *pageViewControllers;
@property(nonatomic,strong)channelTitleView *titleView;
@property(nonatomic, strong)UISwipeGestureRecognizer *rightGesture;
@end
@implementation channelListHomeController
@synthesize pageViewControllers;
@synthesize channelIndex;
@synthesize isOrder;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[MobClick event:@"1"];
self.navigationItem.titleView = self.titleView;
[self leftIconButton:@"Button/backBarBtn.png" highlited:nil selector:@selector(backButtonHandler:)];
[self setPageViews];
self.rightGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightPopSwipeGestureHandler:)];
[self.rightGesture delaysTouchesBegan];
self.rightGesture .numberOfTouchesRequired = 1;
self.rightGesture.delegate = self;
self.rightGesture .direction = UISwipeGestureRecognizerDirectionRight;
// self.rightGesture.enabled = NO;
[self.view addGestureRecognizer:self.rightGesture ];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES; // Recognizers of this class are the first priority
}
- (void)rightPopSwipeGestureHandler:(id)sender {
if (self.channelIndex != 0) {
return;
}
if (!self.isPresent) {
[self.navigationController popViewControllerAnimated:YES];
}else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)backButtonHandler:(id)sender {
if (!self.isPresent) {
[self.navigationController popViewControllerAnimated:YES];
}else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// //在精选界面手指稍微向左滑一点等右上角的“我的话题”字样出现后,迅速向右滑至首页,则再返回精选页面右上角会出现“我的话题”字样,出现后一直存在(bug:4910)
// for (int i = 0; i < self.navigationController.navigationBar.subviews.count; i++) {
// if ([self.navigationController.navigationBar.subviews[i] isKindOfClass:[UILabel class]]) {
// [self.navigationController.navigationBar.subviews[i] removeFromSuperview];
// }
// }
self.navigationController.navigationBarHidden = NO;
if (isIOS7) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
self.navigationController.navigationBar.translucent = YES;
NSDictionary *dic = [TPUserDefault instance].channldataList[self.channelIndex];
[self setNavigationTitleView:dic];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//在精选界面手指稍微向左滑一点等右上角的“我的话题”字样出现后,迅速向右滑至首页,则再返回精选页面右上角会出现“我的话题”字样,出现后一直存在(bug:4910)
for (int i = 0; i < self.navigationController.navigationBar.subviews.count; i++) {
if ([self.navigationController.navigationBar.subviews[i] isKindOfClass:[UILabel class]]) {
[self.navigationController.navigationBar.subviews[i] removeFromSuperview];
}
}
self.navigationController.navigationBarHidden = YES;
}
- (void)setPageViews {
self.pageViewControllers = [NSMutableArray array];
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
if (self.view.bounds.size.width > self.view.bounds.size.height) {
self.view.frame =CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
}
}else {
if (self.view.bounds.size.width < self.view.bounds.size.height) {
self.view.frame =CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
}
}
self.pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
self.pageViewController.view.frame = self.view.bounds;
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
if ([TPUserDefault instance].userBO && self.isOrder) {
for (int i=0;i<7;i++) {
[self addControllerInDataSource:i];
}
}else {
for (int i=0;i<6;i++) {
[self addControllerInDataSource:i];
}
}
[self.pageViewController setViewControllers:@[self.pageViewControllers[self.channelIndex]] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:^(BOOL finished) {
}];
if (self.channelIndex == 0) {
self.rightGesture.enabled = YES;
}else self.rightGesture.enabled = NO;
NSDictionary *dic = [TPUserDefault instance].channldataList[self.channelIndex];
[self setNavigationTitleView:dic];
}
- (void)addControllerInDataSource:(int)index {
NSDictionary *dic = [TPUserDefault instance].channldataList[index];
if([dic[@"nodeId"] isEqualToString:@"-3"]){
customFlowLayout *layout1 = nil;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGSize itemSize = CGSizeZero;
layout1= [[customFlowLayout alloc]init];
layout1.scrollDirection = UICollectionViewScrollDirectionVertical;
layout1.minimumLineSpacing = 35;
layout1.minimumInteritemSpacing = 35;
CGFloat width = 0;
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
width = (self.view.bounds.size.width-35*3)/2;
}else {
width = (self.view.bounds.size.width-35*4)/3;
}
CGFloat viewScale = width/600;
itemSize = CGSizeMake(width, 450*viewScale);
layout1.itemSize = itemSize;
topicListHomeController *vc = [[topicListHomeController alloc] initWithCollectionViewLayout:layout1];
vc.index = index;
[self.pageViewControllers addObject:vc];
[self addChildViewController:vc];
}else{
customFlowLayout *layout = nil;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGSize itemSize = CGSizeZero;
layout= [[customFlowLayout alloc]init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.minimumLineSpacing = 35;
layout.minimumInteritemSpacing = 35;
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGFloat width = (self.view.bounds.size.width-35*3)/2;
itemSize = CGSizeMake(width, 470/2);
}else {
CGFloat width = (self.view.bounds.size.width-35*4)/3;
itemSize = CGSizeMake(width, 446/2);
}
layout.itemSize = itemSize;
channelDetailListController *vc = [[channelDetailListController alloc]initWithCollectionViewLayout:layout];
vc.infoDic = dic;
vc.index = index;
[self.pageViewControllers addObject:vc];
[self addChildViewController:vc];
}
}
- (channelTitleView*)titleView {
if (!_titleView) {
_titleView = [[channelTitleView alloc]initWithFrame:CGRectMake(0, 0, 80, 43)];
}
return _titleView;
}
- (void)setNavigationTitleView:(NSDictionary*)dic {
if ([dic[@"name"] isEqualToString:@"澎湃"]) {
self.titleView.title = @"精选";
}else {
self.titleView.title = dic[@"name"];
}
self.titleView.assistTitle = dic[@"enname"];
self.titleView.pageIndex = self.channelIndex;
if ([TPUserDefault instance].userBO && self.isOrder) {
self.titleView.numsOfPage = 7;
}else {
self.titleView.numsOfPage = 6;
}
if (isIOS7) {
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:dic[@"color"]];
}else {
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:dic[@"color"]];
}
[CoreAnimationEffect animationEaseOut:self.titleView];
}
- (void)statusBarOrientationChange:(NSNotification *)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
if (self.view.bounds.size.width > self.view.bounds.size.height) {
self.view.frame =CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
}
}else {
if (self.view.bounds.size.width < self.view.bounds.size.height) {
self.view.frame =CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
}
}
self.pageViewController.view.frame = self.view.bounds;
if([self.pageViewController.viewControllers[0] isKindOfClass:[topicListHomeController class]]){
// topicListHomeController *vc = self.pageViewController.viewControllers[0];
// TPLOG(@"channelListHomeController---------> x:%f y:%f width:%f height:%f",vc.view.frame.origin.x,vc.view.frame.origin.y,vc.view.frame.size.width,vc.view.frame.size.height);
// [vc pagesDidChange];
}else{
channelDetailListController *vc = self.pageViewController.viewControllers[0];
TPLOG(@"x:%f y:%f width:%f height:%f",vc.view.frame.origin.x,vc.view.frame.origin.y,vc.view.frame.size.width,vc.view.frame.size.height);
[vc pagesDidChange];
}
}
//- (void)currentPageDidChangeInPagingView:(ATPagingView *)pagingView {
// self.channelIndex = pageView.currentPageIndex;
// NSDictionary *dic = [TPUserDefault instance].channldataList[pagingView.currentPageIndex];
// [self setNavigationTitleView:dic];
//
// channelDetailListController *vc = self.pageViewControllers[pagingView.currentPageIndex];
// [vc pagesDidChange];
//}
#pragma mark - page controller delegate and datasource
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController {
NSInteger index;
if ([viewController isKindOfClass:[topicListHomeController class]]) {
index = ((topicListHomeController*)viewController).index;
}else{
index = ((channelDetailListController*)viewController).index;
}
// NSInteger index = ((channelDetailListController*)viewController).index;
if (index == self.pageViewControllers.count-1) {
return nil;
}
if ([viewController isKindOfClass:[topicListHomeController class]]) {
index = ((topicListHomeController*)viewController).index +1;
topicListHomeController *vc = self.pageViewControllers[index];
return vc;
}else{
index = ((channelDetailListController*)viewController).index+1;
channelDetailListController *vc = self.pageViewControllers[index];
return vc;
}
}
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController {
// NSInteger index = ((channelDetailListController*)viewController).index;
NSInteger index;
if ([viewController isKindOfClass:[topicListHomeController class]]) {
index = ((topicListHomeController*)viewController).index;
}else{
index = ((channelDetailListController*)viewController).index;
}
if (index ==0) {
return nil;
}
if ([self.pageViewControllers[index] isKindOfClass:[topicListHomeController class]]) {
index = ((topicListHomeController*)viewController).index - 1;
topicListHomeController *vc = self.pageViewControllers[index];
return vc;
}else{
index = ((channelDetailListController*)viewController).index-1;
channelDetailListController *vc = self.pageViewControllers[index];
return vc;
}
}
- (void)pageViewController:(UIPageViewController *)pageViewController
didFinishAnimating:(BOOL)finished
previousViewControllers:(NSArray *)previousViewControllers
transitionCompleted:(BOOL)completed {
if (completed) {
if([self.pageViewController.viewControllers[0] isKindOfClass:[topicListHomeController class]]){
topicListHomeController *vc = self.pageViewController.viewControllers[0];
self.channelIndex = vc.index;
}else{
channelDetailListController *vc = self.pageViewController.viewControllers[0];
self.channelIndex = vc.index;
}
NSDictionary *dic = [TPUserDefault instance].channldataList[self.channelIndex];
[self setNavigationTitleView:dic];
if (self.channelIndex == 0) {
self.rightGesture.enabled = YES;
}else self.rightGesture.enabled = NO;
}
}
- (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
|