UITableView头部ImageView下拉放大效果,导航栏透明渐变

为了方便自己记忆,赖的详细写了,先贴上代码,有空在详细解读。

这里导航栏透明用的是这位大神的http://tech.glowing.com/cn/change-uinavigationbar-backgroundcolor-dynamically/


#import "ViewController.h"
#import "UINavigationBar+Awesome.h"

#define ImageWidth [[UIScreen mainScreen] bounds].size.width
static CGFloat imageH = 200;
static CGFloat navH = 64;

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) UIImageView *headerView;
@property (nonatomic, strong) UIImage *shadowImage;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:nil];

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.contentInset = UIEdgeInsetsMake(imageH, 0, 0, 0);
    [self.view addSubview:self.tableView];
    
    self.headerView = [[UIImageView alloc]init];
    self.headerView.frame = CGRectMake(0, -imageH, ImageWidth, imageH);
    self.headerView.image = [UIImage imageNamed:@"IMG_0106.JPG"];
    self.headerView.contentMode = UIViewContentModeScaleAspectFill;
    [self.tableView addSubview:self.headerView];
    [self.tableView insertSubview:self.headerView atIndex:0];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.shadowImage = self.navigationController.navigationBar.shadowImage;
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    CGFloat offsetY = self.tableView.contentOffset.y;
    [self changeNavAlphaWithConnentOffset:offsetY];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar lt_reset];
    self.navigationController.navigationBar.shadowImage = self.shadowImage;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"XXXX";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = [[UIViewController alloc]init];
    vc.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    NSLog(@"%f",offsetY);

    if (offsetY < -imageH) {
        NSLog(@"开始改变");
        CGRect f = self.headerView.frame;
        f.origin.y = offsetY;
        f.size.height =  -offsetY;
        self.headerView.frame = f;
    }
    
    [self changeNavAlphaWithConnentOffset:offsetY];
}

-(void)changeNavAlphaWithConnentOffset:(CGFloat)offsetY
{
    UIColor *color = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1];
    if (offsetY > -navH * 2 ) {
        NSLog(@"渐渐不透明");
        CGFloat alpha = MIN(1, 1 - ((-navH * 2 + navH - offsetY) / navH));
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
        self.title = @"个人主页";
    }
    else {
        NSLog(@"渐渐透明");
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
        self.title = @"";
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值