滚动按钮控制器

111

#import "ViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
#import "SixViewController.h"
@interface ViewController ()<UIScrollViewDelegate>{
    UIScrollView *scr;
    UIScrollView *CenterScr;
    UIButton *btn;
}
@property (nonatomic , strong)UIButton *selBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"网易新闻";
    //添加所以子控制器
    [self setUpVc];
    //设置顶部滚动试图
    [self setUp];
    //按钮
    [self setBtn];
    //设置中间滚动试图
    [self setCenter];
}
//设置中间滚动试图
- (void)setCenter{
    CenterScr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height-108)];
    //滚动范围
    CenterScr.contentSize = CGSizeMake(self.view.frame.size.width* self.childViewControllers.count, 0);
    CenterScr.backgroundColor = [UIColor redColor];
    //代理
    CenterScr.delegate = self;
    CenterScr.pagingEnabled = YES;
    CenterScr.showsHorizontalScrollIndicator = NO;
    CenterScr.showsVerticalScrollIndicator = NO;
    CenterScr.bounces = NO;
    [self.view addSubview:CenterScr];
    for (int i= 0 ; i<self.childViewControllers.count; i++) {
        //获取对应的子控制器
        UIViewController *VCL = self.childViewControllers[i];
        //设置子控制器view的位置
        VCL.view.frame = CGRectMake(self.view.frame.size.width*i, 0, self.view.frame.size.width, self.view.frame.size.height-108);
        [CenterScr addSubview:VCL.view];
    }
    
}
//设置顶部滚动试图
- (void)setUp{
    scr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];
    scr.backgroundColor = [UIColor whiteColor];
    scr.showsHorizontalScrollIndicator = NO;
    scr.contentSize = CGSizeMake(6*100, 0);
    [self.view addSubview:scr];
}
//按钮
- (void)setBtn{
    //获取当前控制器的子控制器
    NSInteger count = self.childViewControllers.count;

    for (int i =0; i<count; i++) {
        //初始化位置
        btn = [[UIButton alloc]initWithFrame:CGRectMake(i*100, 0, 100, 44)];
        //绑定标识
        btn.tag = i;
        //获取对应的子控制器的文字
        UIViewController *Vc = self.childViewControllers[i];
        if (i == 0) {
            [self click:btn];
        }
        //添加按钮文字
        [btn setTitle:Vc.title forState:UIControlStateNormal];
        //默认颜色
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //选中后的颜色
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [scr addSubview:btn];
        
    }
}
//按钮方法
- (void)click:(UIButton *)btn{
    [self selBtn:btn];
    //滚动到对应的界面的偏移量
    //    CGFloat offSetX = btn.tag * self.view.frame.size.width;
    //设置中间滚动试图的偏移量
    CenterScr.contentOffset = CGPointMake(btn.tag * self.view.frame.size.width, 0);
    //添加对应自控制器的view到对应的位置
    UIViewController *Vc = self.childViewControllers[btn.tag];
    //设置自控制器view的位置
    Vc.view.frame = CGRectMake(btn.tag * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [CenterScr addSubview:Vc.view];
}

//点击按钮切换颜色
- (void)selBtn:(UIButton *)btn{
    //点击按钮切换颜色
    _selBtn.selected = NO;
    btn.selected = YES;
    _selBtn = btn;
}

//添加所以子控制器
- (void)setUpVc{
    
    //创建热点控制器
    OneViewController *one = [OneViewController new];
    one.title = @"头条";
    //添加到当前控制器
    [self addChildViewController:one];

    
    //创建热点控制器
    TwoViewController *two = [TwoViewController new];
    two.title = @"热点";
    //添加到当前控制器
    [self addChildViewController:two];

    
    //创建热点控制器
    ThreeViewController *three = [ThreeViewController new];
    three.title = @"视频";
    //添加到当前控制器
    [self addChildViewController:three];

    
    //创建热点控制器
    FourViewController *four = [FourViewController new];
    four.title = @"新闻";
    //添加到当前控制器
    [self addChildViewController:four];
 
    
    //创建热点控制器
    FiveViewController *five = [FiveViewController new];
    five.title = @"社会";
    //添加到当前控制器
    [self addChildViewController:five];
    
    
    //创建热点控制器
    SixViewController *Six = [SixViewController new];
    Six.title = @"科技";
    //添加到当前控制器
    [self addChildViewController:Six];
    
}


@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值