// // FXViewController.m // jitao // // Created by 罗云飞 on 2018/8/19. // Copyright © 2018年 罗云飞. All rights reserved. // #import "FXViewController.h" #import "SectionChooseView.h" #import "ZCJDViewController.h" #import "KJZXViewController.h" #import "ZCBKViewController.h" #import "JTKFViewController.h" @interface FXViewController (){ NSString *gender;//模拟性别区分 } //底部滚动ScrollView @property (nonatomic, strong) UIScrollView *contentScrollView; @property(nonatomic,strong)SectionChooseView *sectionChooseView; @end @implementation FXViewController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navgationBar setHidden:YES]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self.navgationBar setHidden:NO]; } - (void)viewDidLoad { [super viewDidLoad]; [self setNavTitle:@"发现"]; [self loaddaohang]; [[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)loaddaohang{ UIView *navgationBar = [[UIView alloc] init]; [navgationBar setFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH, 110)]; [navgationBar setBackgroundColor:[UIColor colorWithString:@"#0097F4"]]; UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH, STATUSBAR_HEIGHT)]; [statusView setBackgroundColor:[UIColor colorWithString:@"#0097F4"]]; // [self.navgationBar addSubview:statusView]; UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)]; [imageview setImage:NewImageNamed(@"bit")]; [navgationBar addSubview:imageview]; // UIImage *backButtonImage = [[UIImage imageNamed:@"fh"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)]; UIButton *backButton= [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(10, 27, 30, 30); [backButton setBackgroundColor:[UIColor clearColor]]; // [backButton setBackgroundImage:NewImageNamed(@"fh") forState:UIControlStateNormal]; [backButton setImage:NewImageNamed(@"fh") forState:UIControlStateNormal]; [backButton addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside]; // [navgationBar addSubview:backButton]; UILabel*navTitleLabel = [[UILabel alloc] init]; [navTitleLabel setFrame:CGRectMake(SCREEN_WIDTH*0.2, 18.0, SCREEN_WIDTH*0.6, NAV_HEIGHT)]; [navTitleLabel setBackgroundColor:NewClearColor]; [navTitleLabel setTextAlignment:NSTextAlignmentCenter]; [navTitleLabel setFont:NewBFont(18)]; [navTitleLabel setTextColor:[UIColor whiteColor]]; [navTitleLabel setText:@"服务项目"]; [navgationBar addSubview:navTitleLabel]; [self.view addSubview:navgationBar]; } - (void)showFirstVC { [self showVc:0]; } #pragma mark -添加所有子控制器 -(void)setupChildViewController { ZCJDViewController *VC1 = [[ZCJDViewController alloc] init]; [self addChildViewController:VC1]; KJZXViewController *VC2 = [[KJZXViewController alloc] init]; [self addChildViewController:VC2]; ZCBKViewController *VC3 = [[ZCBKViewController alloc] init]; [self addChildViewController:VC3]; JTKFViewController *VC4 = [[JTKFViewController alloc] init]; [self addChildViewController:VC4]; } - (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, 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 = fitScreenWidth(14); self.sectionChooseView.selectTitleFont = fitScreenWidth(14); [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