滑动scrollview切换控制器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/helloworld183/article/details/79786991

#import "HGYHomeController.h"

#import "HGYHallViewController.h"

#import "HGYHomePageViewController.h"

#import "UIView+YYExtension.h"

#import "HGYHomeBottomView.h"

@interface HGYHomeController ()<UIScrollViewDelegate>

//控制器名

@property (nonatomic, strong) NSArray *VcNames;

//导航栏

@property(nonatomic, strong) UIView *clickBar;

//选择栏

@property(nonatomic,strong)UIView *titlesView;

//底部容器scrollView

@property (strong, nonatomic) UIScrollView *containerScrollerView;

/**

 *  当前选中的顶部标签内部的按钮

 */

@property(nonatomic,weak)UIButton *selectedButton;


/**

 *  标签底部红色指示器

 */

@property(nonatomic,weak)UIView *indicatorView;


@end


@implementation HGYHomeController

- (UIScrollView *)containerScrollerView

{

    if (!_containerScrollerView) {

        _containerScrollerView = [[UIScrollView alloc]init];

        _containerScrollerView.pagingEnabled = YES;

        _containerScrollerView.showsVerticalScrollIndicator = NO;

        _containerScrollerView.showsHorizontalScrollIndicator = NO;

        _containerScrollerView.contentSize = CGSizeMake(WIDTH *self.VcNames.count,HEIGHT);

        _containerScrollerView.backgroundColor = [UIColor whiteColor];

        _containerScrollerView.delegate = self;

    }

    return _containerScrollerView;

}

//三个子控制器

- (NSArray *)VcNames

{

    if (!_VcNames) {

        

        _VcNames = @[@"首页",@"大厅"];

    }

    return _VcNames;

}



- (void)viewDidLoad {

    [super viewDidLoad];

    [self initTopView];

    

    self.edgesForExtendedLayout = 0;

    //初始化底部scrollView容器

    [self initScrollViewContainer];

    

    //初始化子控制器

    [self addChildControllers];

    

    [self initUI];

}

-(void)initUI{

    

}

-(void)initTopView{

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 44)];

    view.backgroundColor = HGYGlobalBlackColor;

    view.frame = CGRectMake(0, 0, WIDTH, 64);

    [self.view addSubview:view];

    //个人中心

    UIButton *leftButton = [UIButton buttonWithN_name:@"icon_geren" h_name:@"icon_geren" target:self action:@selector(buttonClick:)];

    leftButton.tag = 2;

    leftButton.frame = CGRectMake(0, 20, 40, 44);

    [view addSubview:leftButton];

    //消息

    UIButton *rightButton = [UIButton buttonWithN_name:@"icon_xiaoxi" h_name:@"icon_xiaoxi" target:self action:@selector(buttonClick:)];

    rightButton.tag = 3;

    rightButton.frame = CGRectMake(WIDTH - 40, 20, 40, 44);

    [view addSubview:rightButton];

    

    UIView *titlesView = [[UIView alloc] init];

    titlesView.backgroundColor = [UIColor clearColor];

    titlesView.centerX = view.centerX/2+40;

    titlesView.y = 25;

    titlesView.height = 30;

    titlesView.width = 100;

    [view addSubview:titlesView];

    self.titlesView = titlesView;

    //底部白色指示器

    UIView *indicatorView = [[UIView alloc]init];

    //indicatorView.backgroundColor = [UIColor blueColor];

    indicatorView.backgroundColor = [UIColor whiteColor];

    indicatorView.height = 2;

    indicatorView.tag = -1;

    indicatorView.y = titlesView.height -indicatorView.height;

    self.indicatorView = indicatorView;

    NSArray *titles = @[@"首页",@"大厅"];

    CGFloat height = titlesView.height;

    CGFloat width = titlesView.width/titles.count;

    for (NSInteger i=0;i<2;i++) {

        UIButton *button = [[UIButton alloc] init];

        button.tag = i;

        button.height = height;

        button.width = width;

        button.x = i * button.width;

        button.y = 0;

        [button setTitle:titles[i] forState:UIControlStateNormal];

        [button layoutIfNeeded];

        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];

        button.titleLabel.font = [UIFont systemFontOfSize:14];

        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];

        [titlesView addSubview:button];

        

        //默认点击了第一个按钮

        if (i==0) {

            button.enabled = NO;

            self.selectedButton = button;

            // 让按钮内部的label根据文字内容计算尺寸

            [button.titleLabel sizeToFit];

            self.indicatorView.width = button.titleLabel.width;

            self.indicatorView.centerX = button.centerX;

        }

    }

    [titlesView addSubview:indicatorView];

   

}

//个人中心

-(void)buttonClick:(UIButton *)button{

    NSLog(@"点击按钮");

}

//顶部滑动

-(void)titleClick:(UIButton *)button{

    // 修改按钮的状态

    self.selectedButton.enabled = YES;

    button.enabled = NO;

    self.selectedButton = button;

    

    // 让标签执行动画

    [UIView animateWithDuration:.025 animations:^{

        self.indicatorView.width = self.selectedButton.titleLabel.width;

        self.indicatorView.centerX = self.selectedButton.centerX;

    }];

    

    //container内容的变化

    [self.containerScrollerView setContentOffset:CGPointMake(button.tag *WIDTH, 0) animated:YES];

    

    NSInteger a = button.tag;

    //点击移动控制器

    UIViewController *viewcontroller = self.childViewControllers[a];

    [self.containerScrollerView addSubview:viewcontroller.view];

    viewcontroller.view.frame = CGRectMake(a *WIDTH, 0,WIDTH, HEIGHT);

}



#pragma mark下面的那部份

//初始化滑动容器

- (void)initScrollViewContainer

{

    [self.view addSubview:self.containerScrollerView];

    self.containerScrollerView.frame = CGRectMake(0,64,WIDTH, HEIGHT);

}


//addChildControllers

-(void)addChildControllers{

    HGYHomePageViewController *homepage = [[HGYHomePageViewController alloc]init];

    homepage.view.backgroundColor = [UIColor orangeColor];

    

    [self addChildViewController:homepage];

    HGYHallViewController *hallview = [[HGYHallViewController alloc]init];

    hallview.view.backgroundColor = [UIColor yellowColor];

    [self addChildViewController:hallview];

    //默认展示第一个子控制器

    [self scrollViewDidEndDecelerating:self.containerScrollerView];

}

//滑动减速时调用

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    //获取contentOffset

    CGPoint currentOffset = scrollView.contentOffset;

    

    NSInteger page = currentOffset.x / WIDTH;

    //NSLog(@"输出page的值:%ld",(long)page);

    //取出对应控制器

    UIViewController *viewController = self.childViewControllers[page];

    

    //添加到scrollView容器

    //    if (![viewController isViewLoaded]) {

    

    [self.containerScrollerView addSubview:viewController.view];

    viewController.view.frame = CGRectMake(page *WIDTH, 0,WIDTH, HEIGHT);

}

@end


猜你喜欢

转载自blog.csdn.net/helloworld183/article/details/79786991
今日推荐