iOS 导航栏渐变

我是使用的自定方法来实现导航栏渐变效果的

原理大概是这样的,首先,需要隐藏系统自导的导航栏,然后用 View去代替它,在通过 scrollView 的代理方法来获得当前 scrollView 的滑动偏移量,在改变 view 的透明度,从而实现渐变的效果,好了 话不多述,上代码

//
//  ViewController.m
//  导航栏渐变
//
//  Created by Amydom on 16/8/24.
//  Copyright © 2016年 Amydom. All rights reserved.
//

#import "ViewController.h"
#define IPHONE_WIDTH  [UIScreen mainScreen].bounds.size.width
#define IPHONE_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate , UITableViewDataSource , UIScrollViewDelegate>


@property (nonatomic , strong)UIView *myNavigationView;

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = YES;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //创建导航栏 View
    _myNavigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 64)];
    //预先设置颜色(如果是从有到无,需要预设,相反则不需要)
    _myNavigationView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_myNavigationView];
    
    UITableView *myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, IPHONE_WIDTH, IPHONE_HEIGHT-64) style:UITableViewStylePlain];
   
    myTable.delegate = self;
    myTable.dataSource = self;
    [self.view addSubview:myTable];
    
    [myTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

#pragma mark - tableVeiw Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 3;
    }else{
        return 20;
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0;
    }
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString  *indetifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    cell.textLabel.text = @"jmy";
    return cell;
    
}
#pragma mark - scroll Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    UIColor *color = [UIColor blackColor];
    CGFloat offsetY = scrollView.contentOffset.y;
    //判断开始滑动
    //有到无
    if (offsetY > 0) {
        //根据偏移量来算出对应的 alpha 值
        CGFloat alpha = 1 - (1 - ((64 - offsetY) / 64));
        NSLog(@"alpha : %f , offsetY : %f",alpha,offsetY);
        if (alpha < 0) {
            //小于0的话就直接让他等于0就 OK
            alpha = 0;
        }
        //改变 View 的透明度
        _myNavigationView.backgroundColor = [color colorWithAlphaComponent:alpha];
    }else{
        _myNavigationView.backgroundColor = [color colorWithAlphaComponent:1];
        
    }
  //无到有(需要把之前设置的 color 去掉)
//    if (offsetY > 0) {
//        
//        CGFloat alpha = 1 - ((64 - offsetY) / 64);
//        NSLog(@"alpha : %f , offsetY : %f",alpha,offsetY);
//        if (alpha > 1) {
//            
//            alpha = 1;
//        }
//      
//        _myNavigationView.backgroundColor = [color colorWithAlphaComponent:alpha];
//    }else{
//        _myNavigationView.backgroundColor = [color colorWithAlphaComponent:0];
//        
//    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

直接引用就 OK 啦

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值