FXViewController.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // FXViewController.m
  3. // jitao
  4. //
  5. // Created by 罗云飞 on 2018/8/19.
  6. // Copyright © 2018年 罗云飞. All rights reserved.
  7. //
  8. #import "FXViewController.h"
  9. #import "SectionChooseView.h"
  10. #import "ZCJDViewController.h"
  11. #import "KJZXViewController.h"
  12. #import "ZCBKViewController.h"
  13. #import "JTKFViewController.h"
  14. @interface FXViewController ()<UIScrollViewDelegate,SectionChooseVCDelegate>{
  15. NSString *gender;//模拟性别区分
  16. }
  17. //底部滚动ScrollView
  18. @property (nonatomic, strong) UIScrollView *contentScrollView;
  19. @property(nonatomic,strong)SectionChooseView *sectionChooseView;
  20. @end
  21. @implementation FXViewController
  22. - (void)viewWillAppear:(BOOL)animated
  23. {
  24. [super viewWillAppear:animated];
  25. [self.navgationBar setHidden:YES];
  26. }
  27. - (void)viewDidDisappear:(BOOL)animated
  28. {
  29. [super viewDidDisappear:animated];
  30. [self.navgationBar setHidden:NO];
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. [self setNavTitle:@"发现"];
  35. [self loaddaohang];
  36. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showFirstVC) name:@"ABC" object:nil];
  37. // Do any additional setup after loading the view.
  38. //添加所有子控制器
  39. [self setupChildViewController];
  40. //初始化UIScrollView
  41. [self setupUIScrollView];
  42. // Do any additional setup after loading the view.
  43. }
  44. - (void)loaddaohang{
  45. UIView *navgationBar = [[UIView alloc] init];
  46. [navgationBar setFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH, 110)];
  47. [navgationBar setBackgroundColor:[UIColor colorWithString:@"#0097F4"]];
  48. UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH, STATUSBAR_HEIGHT)];
  49. [statusView setBackgroundColor:[UIColor colorWithString:@"#0097F4"]];
  50. // [self.navgationBar addSubview:statusView];
  51. UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
  52. [imageview setImage:NewImageNamed(@"bit")];
  53. [navgationBar addSubview:imageview];
  54. // UIImage *backButtonImage = [[UIImage imageNamed:@"fh"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
  55. UIButton *backButton= [UIButton buttonWithType:UIButtonTypeCustom];
  56. backButton.frame = CGRectMake(10, 27, 30, 30);
  57. [backButton setBackgroundColor:[UIColor clearColor]];
  58. // [backButton setBackgroundImage:NewImageNamed(@"fh") forState:UIControlStateNormal];
  59. [backButton setImage:NewImageNamed(@"fh") forState:UIControlStateNormal];
  60. [backButton addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
  61. // [navgationBar addSubview:backButton];
  62. UILabel*navTitleLabel = [[UILabel alloc] init];
  63. [navTitleLabel setFrame:CGRectMake(SCREEN_WIDTH*0.2, 18.0, SCREEN_WIDTH*0.6, NAV_HEIGHT)];
  64. [navTitleLabel setBackgroundColor:NewClearColor];
  65. [navTitleLabel setTextAlignment:NSTextAlignmentCenter];
  66. [navTitleLabel setFont:NewBFont(18)];
  67. [navTitleLabel setTextColor:[UIColor whiteColor]];
  68. [navTitleLabel setText:@"服务项目"];
  69. [navgationBar addSubview:navTitleLabel];
  70. [self.view addSubview:navgationBar];
  71. }
  72. - (void)showFirstVC {
  73. [self showVc:0];
  74. }
  75. #pragma mark -添加所有子控制器
  76. -(void)setupChildViewController {
  77. ZCJDViewController *VC1 = [[ZCJDViewController alloc] init];
  78. [self addChildViewController:VC1];
  79. KJZXViewController *VC2 = [[KJZXViewController alloc] init];
  80. [self addChildViewController:VC2];
  81. ZCBKViewController *VC3 = [[ZCBKViewController alloc] init];
  82. [self addChildViewController:VC3];
  83. JTKFViewController *VC4 = [[JTKFViewController alloc] init];
  84. [self addChildViewController:VC4];
  85. }
  86. - (void)setupUIScrollView {
  87. NSArray *arr1 = @[@"政策解读", @"科技资讯",@"资产百科",@"技淘客服"];
  88. // 创建底部滚动视图
  89. self.contentScrollView = [[UIScrollView alloc] init];
  90. _contentScrollView.frame = CGRectMake(0, NavHeader-1, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeader);
  91. _contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width * arr1.count, 0);
  92. _contentScrollView.backgroundColor = [UIColor clearColor];
  93. // 开启分页
  94. _contentScrollView.pagingEnabled = YES;
  95. // 没有弹簧效果
  96. _contentScrollView.bounces = NO;
  97. // 隐藏水平滚动条
  98. _contentScrollView.showsHorizontalScrollIndicator = NO;
  99. // 设置代理
  100. _contentScrollView.delegate = self;
  101. [self.view addSubview:_contentScrollView];
  102. self.sectionChooseView = [[SectionChooseView alloc] initWithFrame:CGRectMake(0, NavHeader, self.view.frame.size.width, NavHeader) titleArray:arr1];
  103. self.sectionChooseView.selectIndex = 0;
  104. self.sectionChooseView.delegate = self;
  105. self.sectionChooseView.normalBackgroundColor = NewClearColor;
  106. self.sectionChooseView.selectBackgroundColor = [UIColor whiteColor];;
  107. self.sectionChooseView.titleNormalColor = [UIColor whiteColor];
  108. self.sectionChooseView.titleSelectColor = [UIColor colorWithString:@"#0097F4"];
  109. self.sectionChooseView.normalTitleFont = fitScreenWidth(14);
  110. self.sectionChooseView.selectTitleFont = fitScreenWidth(14);
  111. [self.view addSubview:self.sectionChooseView];
  112. }
  113. #pragma mark -SMCustomSegmentDelegate
  114. - (void)SectionSelectIndex:(NSInteger)selectIndex {
  115. NSLog(@"---------%ld",(long)selectIndex);
  116. // 1 计算滚动的位置
  117. CGFloat offsetX = selectIndex * self.view.frame.size.width;
  118. self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
  119. // 2.给对应位置添加对应子控制器
  120. [self showVc:selectIndex];
  121. }
  122. #pragma mark -显示控制器的view
  123. /**
  124. * 显示控制器的view
  125. *
  126. * @param index 选择第几个
  127. *
  128. */
  129. - (void)showVc:(NSInteger)index {
  130. CGFloat offsetX = index * self.view.frame.size.width;
  131. UIViewController *vc = self.childViewControllers[index];
  132. // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
  133. if (vc.isViewLoaded) return;
  134. [self.contentScrollView addSubview:vc.view];
  135. vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
  136. }
  137. #pragma mark -UIScrollViewDelegate
  138. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  139. // 计算滚动到哪一页
  140. NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
  141. // 1.添加子控制器view
  142. [self showVc:index];
  143. // 2.把对应的标题选中
  144. self.sectionChooseView.selectIndex = index;
  145. }
  146. - (void)didReceiveMemoryWarning {
  147. [super didReceiveMemoryWarning];
  148. // Dispose of any resources that can be recreated.
  149. }
  150. /*
  151. #pragma mark - Navigation
  152. // In a storyboard-based application, you will often want to do a little preparation before navigation
  153. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  154. // Get the new view controller using [segue destinationViewController].
  155. // Pass the selected object to the new view controller.
  156. }
  157. */
  158. @end