NewMycollectionViewController.m 4.9 KB

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