123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- //
- // SegmentViewController.m
- // CLSegment
- //
- // Created by CL on 2018/4/9.
- // Copyright © 2018年 cl. All rights reserved.
- //
- #import "SegmentViewController.h"
- #define HEADBTN_TAG 10000
- @interface SegmentViewController ()
- @property (nonatomic, strong) UIScrollView *headerView;
- @property (nonatomic, strong) UIScrollView *mainScrollView;
- @end
- @implementation SegmentViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- - (void)initSegment:(CGFloat)height
- {
- [self addButtonInScrollHeader:_titleArray height:height];
- [self addContentViewScrollView:_subViewControllers height:height];
- }
- /*!
- * @brief 根据传入的title数组新建button显示在顶部scrollView上
- *
- * @param titleArray title数组
- */
- - (void)addButtonInScrollHeader:(NSArray *)titleArray height:(CGFloat)height
- {
- self.headerView.frame = CGRectMake(0, height, SCREEN_WIDTH, self.buttonHeight);
- if (_segmentHeaderType == 0) {
- self.headerView.contentSize = CGSizeMake(self.buttonWidth * titleArray.count, self.buttonHeight);
- }
- else {
- self.headerView.contentSize = CGSizeMake(SCREEN_WIDTH, self.buttonHeight);
- }
- [self.view addSubview:self.headerView];
-
- for (NSInteger index = 0; index < titleArray.count; index++) {
- UIButton *segmentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- segmentBtn.frame = CGRectMake(self.buttonWidth * index, 0, self.buttonWidth, self.buttonHeight);
- [segmentBtn setTitle:titleArray[index] forState:UIControlStateNormal];
- segmentBtn.titleLabel.font = [UIFont systemFontOfSize:self.fontSize];
- segmentBtn.tag = index + HEADBTN_TAG;
- [segmentBtn setTitleColor:self.titleColor forState:UIControlStateNormal];
- [segmentBtn setTitleColor:self.titleSelectedColor forState:UIControlStateSelected];
- [segmentBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- [self.headerView addSubview:segmentBtn];
- if (index == 0) {
- segmentBtn.selected = YES;
- self.selectIndex = segmentBtn.tag;
- }
- }
- }
- /*!
- * @brief 根据传入的viewController数组,将viewController的view添加到显示内容的scrollView
- *
- * @param subViewControllers viewController数组
- */
- - (void)addContentViewScrollView:(NSArray *)subViewControllers height:(CGFloat)height
- {
- _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.buttonHeight + height , SCREEN_WIDTH, SCREEN_HEIGHT - height - self.buttonHeight)];
- _mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * subViewControllers.count, SCREEN_HEIGHT - height - self.buttonHeight);
- [_mainScrollView setScrollEnabled:NO];
-
- [_mainScrollView setShowsVerticalScrollIndicator:NO];
- [_mainScrollView setShowsHorizontalScrollIndicator:NO];
- _mainScrollView.bounces = NO;
-
- [self.view addSubview:_mainScrollView];
-
- [subViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
-
- UIViewController *viewController = (UIViewController *)_subViewControllers[idx];
- viewController.view.frame = CGRectMake(idx * SCREEN_WIDTH, 0, SCREEN_WIDTH, _mainScrollView.frame.size.height);
-
- [_mainScrollView addSubview:viewController.view];
- [self addChildViewController:viewController];
- }];
- }
- - (void)addParentController:(UIViewController *)viewController
- {
- if ([viewController respondsToSelector:@selector(edgesForExtendedLayout)])
- {
- viewController.edgesForExtendedLayout = UIRectEdgeNone;
- }
- [viewController addChildViewController:self];
- [viewController.view addSubview:self.view];
- }
- #pragma mark 点击按钮方法
- - (void)btnClick:(UIButton *)button
- {
- [_mainScrollView scrollRectToVisible:CGRectMake((button.tag - HEADBTN_TAG) *SCREEN_WIDTH, 0, SCREEN_WIDTH, _mainScrollView.frame.size.height) animated:NO];
- [self didSelectSegmentIndex:button.tag];
- }
- /*!
- * @brief 设置顶部选中button下方线条位置
- *
- * @param index 第几个
- */
- - (void)didSelectSegmentIndex:(NSInteger)index
- {
- UIButton *btn = (UIButton *)[self.view viewWithTag:self.selectIndex];
- btn.selected = NO;
- self.selectIndex = index;
- UIButton *currentSelectBtn = (UIButton *)[self.view viewWithTag:index];
- currentSelectBtn.selected = YES;
- }
- #pragma mark - ScrollViewDelegate
- //- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- //{
- // if (scrollView.contentOffset.x<=960) {
- // if (scrollView == _mainScrollView) {
- // float xx = scrollView.contentOffset.x * (_buttonWidth / SCREEN_WIDTH) - _buttonWidth;
- // [_headerView scrollRectToVisible:CGRectMake(xx, 0, SCREEN_WIDTH, _headerView.frame.size.height) animated:YES];
- // NSInteger currentIndex = scrollView.contentOffset.x / SCREEN_WIDTH;
- // [self didSelectSegmentIndex:currentIndex + HEADBTN_TAG];
- // }
- // }else{
- // [scrollView setScrollEnabled:NO];
- // }
- //}
- //
- //- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
- //{
- // float xx = scrollView.contentOffset.x * (_buttonWidth / SCREEN_WIDTH) - _buttonWidth;
- // [_headerView scrollRectToVisible:CGRectMake(xx, 0, SCREEN_WIDTH, _headerView.frame.size.height) animated:YES];
- //}
- #pragma mark - setter/getter
- - (UIScrollView *)headerView
- {
- if (_headerView == nil) {
- _headerView = [[UIScrollView alloc] init];
- [_headerView setShowsVerticalScrollIndicator:NO];
- [_headerView setShowsHorizontalScrollIndicator:NO];
- _headerView.bounces = NO;
- _headerView.backgroundColor = self.headViewBackgroundColor;
- }
- return _headerView;
- }
- - (UIColor *)headViewBackgroundColor
- {
- if (_headViewBackgroundColor == nil) {
- _headViewBackgroundColor = UIColorHex(0xFFFFFF);
- }
- return _headViewBackgroundColor;
- }
- - (UIColor *)titleColor
- {
- if (_titleColor == nil) {
- _titleColor = UIColorHex(0x0A0A0A);
- }
- return _titleColor;
- }
- - (UIColor *)titleSelectedColor
- {
- if (_titleSelectedColor == nil) {
- _titleSelectedColor = UIColorHex(0x3979D3);
- }
- return _titleSelectedColor;
- }
- - (CGFloat)fontSize
- {
- if (_fontSize == 0) {
- _fontSize = 15.f;
- }
- return _fontSize;
- }
- - (CGFloat)buttonWidth
- {
- if (_buttonWidth == 0) {
- _buttonWidth = SCREEN_WIDTH / 4;
- }
- return _buttonWidth;
- }
- - (CGFloat)buttonHeight
- {
- if (_buttonHeight == 0) {
- _buttonHeight = 50.f;
- }
- return _buttonHeight;
- }
- - (CGFloat)bottomLineHeight
- {
- if (_bottomLineHeight == 0) {
- _bottomLineHeight = 1.f;
- }
- return _bottomLineHeight;
- }
- - (CGFloat)bottomCount
- {
- if (_bottomCount == 0) {
- _bottomCount = 1;
- }
- return _bottomCount;
- }
- - (UIColor *)bottomLineColor
- {
- if (_bottomLineColor == nil) {
- _bottomLineColor = [UIColor whiteColor];
- }
- return _bottomLineColor;
- }
- @end
|