// // NewMyfollowViewController.m // jitao // // Created by 罗云飞 on 2018/8/11. // Copyright © 2018年 罗云飞. All rights reserved. // #import "NewMyfollowViewController.h" #import "SectionChooseView.h" #import "NewfollowadviserViewController.h" #import "NewfollowexpertViewController.h" @interface NewMyfollowViewController (){ NSString *gender;//模拟性别区分 } //底部滚动ScrollView @property (nonatomic, strong) UIScrollView *contentScrollView; @property(nonatomic,strong)SectionChooseView *sectionChooseView; @end @implementation NewMyfollowViewController - (void)viewDidLoad { [super viewDidLoad]; [self setNavTitle:@"我的关注"]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showFirstVC) name:@"ABC" object:nil]; // Do any additional setup after loading the view. //添加所有子控制器 [self setupChildViewController]; //初始化UIScrollView [self setupUIScrollView]; // Do any additional setup after loading the view. } - (void)showFirstVC { [self showVc:0]; } #pragma mark -添加所有子控制器 -(void)setupChildViewController { NewfollowadviserViewController *VC1 = [[NewfollowadviserViewController alloc] init]; [self addChildViewController:VC1]; NewfollowexpertViewController *VC2 = [[NewfollowexpertViewController alloc] init]; [self addChildViewController:VC2]; } - (void)setupUIScrollView { NSArray *arr1 = @[@"专家", @"顾问"]; // 创建底部滚动视图 self.contentScrollView = [[UIScrollView alloc] init]; _contentScrollView.frame = CGRectMake(0, NavHeader-1, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeader); _contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width * arr1.count, 0); _contentScrollView.backgroundColor = [UIColor clearColor]; // 开启分页 _contentScrollView.pagingEnabled = YES; // 没有弹簧效果 _contentScrollView.bounces = NO; // 隐藏水平滚动条 _contentScrollView.showsHorizontalScrollIndicator = NO; // 设置代理 _contentScrollView.delegate = self; [self.view addSubview:_contentScrollView]; self.sectionChooseView = [[SectionChooseView alloc] initWithFrame:CGRectMake(0, NavHeader-1, self.view.frame.size.width, NavHeader) titleArray:arr1]; self.sectionChooseView.selectIndex = 0; self.sectionChooseView.delegate = self; self.sectionChooseView.normalBackgroundColor = NewClearColor; self.sectionChooseView.selectBackgroundColor = [UIColor whiteColor];; self.sectionChooseView.titleNormalColor = [UIColor whiteColor]; self.sectionChooseView.titleSelectColor = [UIColor colorWithString:@"#0097F4"]; self.sectionChooseView.normalTitleFont = 16; self.sectionChooseView.selectTitleFont = 16; self.sectionChooseView.backgroundColor = NewClearColor; [self.view addSubview:self.sectionChooseView]; } #pragma mark -SMCustomSegmentDelegate - (void)SectionSelectIndex:(NSInteger)selectIndex { NSLog(@"---------%ld",(long)selectIndex); // 1 计算滚动的位置 CGFloat offsetX = selectIndex * self.view.frame.size.width; self.contentScrollView.contentOffset = CGPointMake(offsetX, 0); // 2.给对应位置添加对应子控制器 [self showVc:selectIndex]; } #pragma mark -显示控制器的view /** * 显示控制器的view * * @param index 选择第几个 * */ - (void)showVc:(NSInteger)index { CGFloat offsetX = index * self.view.frame.size.width; UIViewController *vc = self.childViewControllers[index]; // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载 if (vc.isViewLoaded) return; [self.contentScrollView addSubview:vc.view]; vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height); } #pragma mark -UIScrollViewDelegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ // 计算滚动到哪一页 NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width; // 1.添加子控制器view [self showVc:index]; // 2.把对应的标题选中 self.sectionChooseView.selectIndex = index; } - (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