ios 两个分段控制器

2018-11-21  本文已影响0人  你又脸红了i

#define KUIScreenWidth [UIScreen mainScreen].bounds.size.width

#define KUIScreenHeight [UIScreen mainScreen].bounds.size.height

<UIScrollViewDelegate,SectionChooseVCDelegate>

//底部滚动ScrollView

@property(nonatomic,strong)UIScrollView*contentScrollView;

@property(nonatomic,strong)SectionChooseView *sectionChooseView;

  self.view.backgroundColor = [UIColor yellowColor];

    // 首次进入加载第一个界面通知

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showFirstVC) name:@"ABC" object:nil];

  //添加所有子控制器

    [self setupChildViewController];

    //初始化UIScrollView

    [self setupUIScrollView];

}

- (void)showFirstVC {

    [selfshowVc:0];

}

- (void)setupUIScrollView {

    // 创建底部滚动视图

    self.contentScrollView = [[UIScrollView alloc] init];

    _contentScrollView.frame = CGRectMake(0, 44, KUIScreenWidth, KUIScreenHeight);

    _contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 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, 88, self.view.frame.size.width, 44) titleArray:@[@"全部", @"返厂精选"]];

    self.sectionChooseView.selectIndex = 0;

    self.sectionChooseView.delegate = self;

    //self.sectionChooseView.normalBackgroundColor = [UIColor whiteColor];

    //self.sectionChooseView.selectBackgroundColor = [UIColor orangeColor];;

    self.sectionChooseView.titleNormalColor = [UIColor orangeColor];

    self.sectionChooseView.titleSelectColor = [UIColor orangeColor];

    self.sectionChooseView.normalTitleFont = 18;

    self.sectionChooseView.selectTitleFont = 20;

    [self.view addSubview:self.sectionChooseView];

}

#pragma mark -添加所有子控制器

-(void)setupChildViewController {

    yiViewController *voucherUnusedVC = [[yiViewController alloc] init];

    [selfaddChildViewController:voucherUnusedVC];

    erViewController *voucherUsedVC = [[erViewController alloc] init];

    [self addChildViewController:voucherUsedVC];

}

#pragma mark -SMCustomSegmentDelegate

- (void)SectionSelectIndex:(NSInteger)selectIndex {

    NSLog(@"---------%ld",(long)selectIndex);

    // 1 计算滚动的位置

    CGFloatoffsetX = selectIndex *self.view.frame.size.width;

    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);

    // 2.给对应位置添加对应子控制器

    [selfshowVc:selectIndex];

}

#pragma mark -显示控制器的view

/**

 *  显示控制器的view

 *

 *  @paramindex 选择第几个

 *

 */

- (void)showVc:(NSInteger)index {

    CGFloatoffsetX = 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{

    // 计算滚动到哪一页

    NSIntegerindex = scrollView.contentOffset.x/ scrollView.frame.size.width;

    // 1.添加子控制器view

    [selfshowVc:index];

    // 2.把对应的标题选中

    self.sectionChooseView.selectIndex = index;

}

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

上一篇下一篇

猜你喜欢

热点阅读