NewMyfollowViewController.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // NewMyfollowViewController.m
  3. // jitao
  4. //
  5. // Created by 罗云飞 on 2018/8/11.
  6. // Copyright © 2018年 罗云飞. All rights reserved.
  7. //
  8. #import "NewMyfollowViewController.h"
  9. #import "SectionChooseView.h"
  10. #import "NewfollowadviserViewController.h"
  11. #import "NewfollowexpertViewController.h"
  12. @interface NewMyfollowViewController ()<UIScrollViewDelegate,SectionChooseVCDelegate>{
  13. NSString *gender;//模拟性别区分
  14. }
  15. //底部滚动ScrollView
  16. @property (nonatomic, strong) UIScrollView *contentScrollView;
  17. @property(nonatomic,strong)SectionChooseView *sectionChooseView;
  18. @end
  19. @implementation NewMyfollowViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. [self setNavTitle:@"我的关注"];
  23. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showFirstVC) name:@"ABC" object:nil];
  24. // Do any additional setup after loading the view.
  25. //添加所有子控制器
  26. [self setupChildViewController];
  27. //初始化UIScrollView
  28. [self setupUIScrollView];
  29. // Do any additional setup after loading the view.
  30. }
  31. - (void)showFirstVC {
  32. [self showVc:0];
  33. }
  34. #pragma mark -添加所有子控制器
  35. -(void)setupChildViewController {
  36. NewfollowadviserViewController *VC1 = [[NewfollowadviserViewController alloc] init];
  37. [self addChildViewController:VC1];
  38. NewfollowexpertViewController *VC2 = [[NewfollowexpertViewController alloc] init];
  39. [self addChildViewController:VC2];
  40. }
  41. - (void)setupUIScrollView {
  42. NSArray *arr1 = @[@"专家", @"顾问"];
  43. // 创建底部滚动视图
  44. self.contentScrollView = [[UIScrollView alloc] init];
  45. _contentScrollView.frame = CGRectMake(0, NavHeader-1, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeader);
  46. _contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width * arr1.count, 0);
  47. _contentScrollView.backgroundColor = [UIColor clearColor];
  48. // 开启分页
  49. _contentScrollView.pagingEnabled = YES;
  50. // 没有弹簧效果
  51. _contentScrollView.bounces = NO;
  52. // 隐藏水平滚动条
  53. _contentScrollView.showsHorizontalScrollIndicator = NO;
  54. // 设置代理
  55. _contentScrollView.delegate = self;
  56. [self.view addSubview:_contentScrollView];
  57. self.sectionChooseView = [[SectionChooseView alloc] initWithFrame:CGRectMake(0, NavHeader-1, self.view.frame.size.width, NavHeader) titleArray:arr1];
  58. self.sectionChooseView.selectIndex = 0;
  59. self.sectionChooseView.delegate = self;
  60. self.sectionChooseView.normalBackgroundColor = NewClearColor;
  61. self.sectionChooseView.selectBackgroundColor = [UIColor whiteColor];;
  62. self.sectionChooseView.titleNormalColor = [UIColor whiteColor];
  63. self.sectionChooseView.titleSelectColor = [UIColor colorWithString:@"#0097F4"];
  64. self.sectionChooseView.normalTitleFont = 16;
  65. self.sectionChooseView.selectTitleFont = 16;
  66. self.sectionChooseView.backgroundColor = NewClearColor;
  67. [self.view addSubview:self.sectionChooseView];
  68. }
  69. #pragma mark -SMCustomSegmentDelegate
  70. - (void)SectionSelectIndex:(NSInteger)selectIndex {
  71. NSLog(@"---------%ld",(long)selectIndex);
  72. // 1 计算滚动的位置
  73. CGFloat offsetX = selectIndex * self.view.frame.size.width;
  74. self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
  75. // 2.给对应位置添加对应子控制器
  76. [self showVc:selectIndex];
  77. }
  78. #pragma mark -显示控制器的view
  79. /**
  80. * 显示控制器的view
  81. *
  82. * @param index 选择第几个
  83. *
  84. */
  85. - (void)showVc:(NSInteger)index {
  86. CGFloat offsetX = index * self.view.frame.size.width;
  87. UIViewController *vc = self.childViewControllers[index];
  88. // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
  89. if (vc.isViewLoaded) return;
  90. [self.contentScrollView addSubview:vc.view];
  91. vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
  92. }
  93. #pragma mark -UIScrollViewDelegate
  94. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  95. // 计算滚动到哪一页
  96. NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
  97. // 1.添加子控制器view
  98. [self showVc:index];
  99. // 2.把对应的标题选中
  100. self.sectionChooseView.selectIndex = index;
  101. }
  102. - (void)didReceiveMemoryWarning {
  103. [super didReceiveMemoryWarning];
  104. // Dispose of any resources that can be recreated.
  105. }
  106. /*
  107. #pragma mark - Navigation
  108. // In a storyboard-based application, you will often want to do a little preparation before navigation
  109. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  110. // Get the new view controller using [segue destinationViewController].
  111. // Pass the selected object to the new view controller.
  112. }
  113. */
  114. @end