用ScrollView设置导航栏滚动时图

用ScrollView设置导航栏滚动时图,代码如下:

#define HScreen [[UIScreen mainScreen] bounds].size.height
#define WScreen [[UIScreen mainScreen] bounds].size.width

@interface WHINewsViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scollerTop;
@property (nonatomic,strong)UIScrollView *scollerView;

@end

@implementation WHINewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *title= @[@"兑换中",@"兑换中",@"即将开始",@"即将开始",@"即将开始"];
    NSArray *title2= @[@"9:00",@"11:00",@"15:00",@"17:00",@"20:00"];
    self.scollerTop.bounces = NO;
    self.scollerTop.showsHorizontalScrollIndicator =NO;
    self.scollerTop.showsVerticalScrollIndicator =NO;
//    self.scollerTop.frame = CGRectMake(0, 0, WScreen, 48);
    self.scollerTop = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WScreen, 48)];
    self.scollerTop.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.scollerTop];

    _scollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 48, WScreen, HScreen-48)];
    _scollerView.showsHorizontalScrollIndicator = NO;
    _scollerView.showsVerticalScrollIndicator = NO;
    _scollerView.bounces = NO;
    _scollerView.pagingEnabled = YES;
    _scollerView.delegate = self;
//    _scollerView.pagingEnabled = YES;
    _scollerView.userInteractionEnabled = YES;
//    _scollerView.showsHorizontalScrollIndicator = NO;
    _scollerView.contentSize = CGSizeMake(5*WScreen, 0);
    [self.view addSubview:_scollerView];

    self.scollerTop.tag = 1;
    for (int i= 0; i<title.count; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*WScreen/title.count, 0, WScreen/title.count, 46);
        btn.titleLabel.font = [UIFont systemFontOfSize:11];
        btn.tag = i+100;
        [btn addTarget:self action:@selector(actionBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.scollerTop addSubview:btn];

        UILabel *ScrollTopLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(i*WScreen/title.count, 23, WScreen/title.count, 20)];
        ScrollTopLabel1.textAlignment = NSTextAlignmentCenter;
        [ScrollTopLabel1 setText:title[i]];
        ScrollTopLabel1.tag = i+300;
        [ScrollTopLabel1 setTextColor:[UIColor blackColor]];
        ScrollTopLabel1.font = [UIFont systemFontOfSize:11];
        [self.scollerTop addSubview:ScrollTopLabel1];


        UILabel *ScrollTopLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(i*WScreen/title.count, 6, WScreen/title.count, 20)];
        ScrollTopLabel2.textAlignment = NSTextAlignmentCenter;
        [ScrollTopLabel2 setText:title2[i]];
        ScrollTopLabel2.tag = i+400;
        [ScrollTopLabel2 setTextColor:[UIColor blackColor]];
        ScrollTopLabel2.font = [UIFont systemFontOfSize:16];
        [self.scollerTop addSubview:ScrollTopLabel2];

    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 46, WScreen/title.count, 2)];
    view.backgroundColor = [UIColor redColor];
    view.tag =200;
    [self.scollerTop addSubview:view];
    for (int i=0; i<title.count; i++) {
       UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(i*WScreen, 0, WScreen,_scollerView.frame.size.height)];

        if (i==0){
//        [view1 addSubview:vc1.view];
            view1.backgroundColor = [UIColor redColor];
        }
        else if(i==1){
//         [view1 addSubview:vc2.view];
            view1.backgroundColor = [UIColor yellowColor];
        }else if (i==2){
//         [view1 addSubview:vc3.view];
            view1.backgroundColor = [UIColor grayColor];
        }else if (i==3){
//         [view1 addSubview:vc4.view];
            view1.backgroundColor = [UIColor greenColor];
        }else if (i==4){
//         [view1 addSubview:vc5.view];
            view1.backgroundColor = [UIColor blueColor];
        }

        [_scollerView addSubview:view1];
    }
}

#pragma  mark action 点击
-(void)actionBtn:(UIButton *)btn{

    [UIView animateWithDuration:0.2 animations:^{
        UIScrollView *view1 = (UIScrollView *)[self.view viewWithTag:200] ;
        view1.frame = CGRectMake(WScreen/5*(btn.tag-100), 46, WScreen/5, 2);

        for (int i=0; i<5; i++) {
            UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(i+300)];
            ScrollTopLabel1.textColor = [UIColor blackColor];
        }
        for (int i=0; i<5; i++) {
            UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(i+400)];
            ScrollTopLabel1.textColor = [UIColor blackColor];
        }
        UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(btn.tag+200)];
        ScrollTopLabel1.textColor = [UIColor redColor];
        UILabel *ScrollTopLabel2 = (UILabel *)[self.scollerTop viewWithTag:(btn.tag+300)];
        ScrollTopLabel2.textColor = [UIColor redColor];
    }];
    _scollerView.contentOffset = CGPointMake(WScreen *(btn.tag-100), 0);
    NSLog(@"%lf",_scollerView.contentOffset.x/WScreen);
}

#pragma mark  scrollV 的滑动事件
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (_scollerView ==scrollView) {
        int xx = _scollerView.contentOffset.x/WScreen;
        NSLog(@"++++++++>%lf",_scollerView.contentOffset.x/WScreen);
        [UIView animateWithDuration:0.2 animations:^{
            UIView *view1 = (UIView *)[self.scollerTop viewWithTag:200];
            view1.frame = CGRectMake(WScreen/5*xx, 46, WScreen/5, 2);
            for (int i=0; i<5; i++) {
                UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(i+300)];
                ScrollTopLabel1.textColor = [UIColor blackColor];
            }
            for (int i=0; i<5; i++) {
                UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(i+400)];
                ScrollTopLabel1.textColor = [UIColor blackColor];
            }
            UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(xx+300)];
            ScrollTopLabel1.textColor = [UIColor redColor];
            UILabel *ScrollTopLabel2 = (UILabel *)[self.scollerTop viewWithTag:(xx+400)];
            ScrollTopLabel2.textColor = [UIColor redColor];
        }];
    }
}

@end

效果图如下:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值