我是使用的自定方法来实现导航栏渐变效果的
原理大概是这样的,首先,需要隐藏系统自导的导航栏,然后用 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 啦