iOS 上下滑动tableView导航透明度逐渐改变

来源:互联网 发布:己知线段AB=12,CD=6 编辑:程序博客网 时间:2024/05/18 01:08

demo下载地址:http://download.csdn.net/download/u010981736/9934641

效果如下:

刚进入页面

向下滑动

完全不透明

核心代码:

////  ViewController.m//  渐变导航////  Created by llkj on 2017/8/15.//  Copyright © 2017年 LayneCheung. All rights reserved.//#import "ViewController.h"#import "UIImage+Image.h"#define HeaderH 200#define TarBarH 44#define MinH 64@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>@property (weak, nonatomic) IBOutlet UITableView *tableView;@property(nonatomic, weak) UILabel *titleL;@property(nonatomic ,assign) CGFloat oriOffsetY;@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerHeightConts;@end@implementation ViewControllerstatic NSString *ID = @"cell";- (void)viewDidLoad {    [super viewDidLoad];    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];    //iOS7之后,只要是导航控制器下的所有UIScrollView顶部都会添加额外的滚动区域.    //设置当前控制器不要调整ScrollView的contentInsets    self.automaticallyAdjustsScrollViewInsets = NO;    //设置导航条隐藏    //但是我们这里导航条隐藏不是直接隐藏的,而是有一个透明度, 根据滚到的位置,设置透明度的.    //self.navigationController.navigationBar.hidden = YES;    //设置导航条透明度为0    //设置导航条透明度为0,没有效果,还是原来的样子.    //原因是因为导航条上面那一块并不直接是导航条,它是导航条里面的一个子控件.    //所以在这里设置它没有效果,因为系统会生成一个半透明的图片.    self.navigationController.navigationBar.alpha = 0;    //所以在这里我们可以考虑给它设置一个半透明的图片.    //在这里,有一个模式,必须要传默认UIBarMetricsDefault模式.    //在这里发现设为nil的时候,也没有效果,那是因为系统它做了一层判断,它会判断如果传入的系统图片为空的话,它就会帮你生成一个半透明的图片,设置导航条的背景图片.    //[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];    //那在这里传入一张空的图片,然后就有效果了.    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];    //但是设置完后,发现有一根线,这根线其实是导航条的一个阴影.直接把它清空就行了.    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];    //设置tableView的数据源    self.tableView.dataSource = self;    //设置tableView的代理    self.tableView.delegate = self;    self.oriOffsetY = -(HeaderH + TarBarH);    //设置UITableView的默认额外顶部滚动区域244.    //tableView只要设置额外的滚动区域,就会把内容往下面挤.    self.tableView.contentInset = UIEdgeInsetsMake(HeaderH + TarBarH, 0, 0, 0);    //导航条上的View不允许直接设为透明.    UILabel *titleL = [[UILabel alloc] init];    titleL.textColor = [UIColor colorWithWhite:0 alpha:0];    titleL.text = @"渐变导航";    [titleL sizeToFit];    self.titleL = titleL;    //可以把文字的颜色搞成透明.    self.navigationItem.titleView = titleL;}//只要tableView滚动时,就会调用这个方法-(void)scrollViewDidScroll:(UIScrollView *)scrollView{    //计算滚动的便宜量    CGFloat offSetY = scrollView.contentOffset.y - self.oriOffsetY;    NSLog(@"%f",offSetY);    //在这里不能使用距离上面的约束,因为这个地方我们是想要有一个视觉差的效果,还有就是要保证一直距离顶部为0的大小.    //self.headerTopCons.constant = - offSetY;    CGFloat h = HeaderH - offSetY;    if (h < 64) {        h = 64;    }    self.headerHeightConts.constant = h;    // 处理导航条    // 计算当前的透明度    CGFloat alpha = offSetY / (HeaderH - MinH);    if (alpha > 1) {        alpha = 0.99;    }    //设置标题的透明度    self.titleL.textColor = [UIColor colorWithWhite:0 alpha:alpha];    // 获取导航条的颜色    UIColor *navColor = [UIColor colorWithWhite:1 alpha:alpha];    // 设置导航条背景图片    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:navColor] forBarMetrics:UIBarMetricsDefault];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 30;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];    cell.textLabel.text = @"LayneCheung";    return cell;}@end
阅读全文
0 0