iOS-导航头像缩放,支持点击回调

在很多App中,经常存在一种需求就是,界面上下滚动时用户的头像也会跟着滚动,而用户头像在视图向上滚动一定范围时停留并在导航栏的位置

基本用法如下:
1、单纯的实现这一效果:

- (LEOHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[LEOHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kImageHeight)];
        [_headerView setBackgroundImage:[UIImage imageNamed:@"background.jpg"]];
        [_headerView setHeaderImage:[UIImage imageNamed:@"header.jpg"] text:@"leiliang"];
    }
    return _headerView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.headerView reloadWithScrollView:scrollView];
}

2、给头像添加点击回调:

[self.headerView pressHeaderImageWithBlock:^{
    NSLog(@"点击头像");
}];

3、在视图滚动到顶部位置时改变navigationBar的颜色:

// navigationBar 的颜色可以根据这个方法来调整
// @param reachtop: YES 已经滚动到顶部, NO 在顶部以下
__weak typeof(self) weakSelf = self;
[self.headerView scrollViewStateChangeWithBlock:^(BOOL reachtop) {
    [weakSelf.navigationController.navigationBar leo_setBackgroundColor:reachtop ? [UIColor lightGrayColor] : [UIColor clearColor]];
}];

使用起来很简单,只需要这几行代码就可以了,另外为了防止大家在使用过程中因为一些细节问题导致有bug出现,这里列出一些注意事项,只要大家按照这样做就一定没有问题了,具体如下:

 1、在 - (void)viewWillAppear:(BOOL)animated 方法中需要调用一次 [self.headerView reloadWithScrollView:self.tableView],为了防止在刚进入这一页面的时候视图有偏差
 2、需要设置 tableView 的 contentInset 的 top 值为 kImageHeight - kNavigationBarHeight
 3、需要设置 tableView 的背景色为 [UIColor clearColor],否则会遮挡视图
 4、需要设置 tableView 的顶部据屏幕顶部为 64,否则如果你想设置navigationBar为透明时顶部有留白
 5、需要将视图插入到 tableView 的底部,这里是将视图加在 self.view 上,并在tableView的底部,[self.view insertSubview:self.headerView belowSubview:self.tableView]
 6、需要在 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 方法中调用 [self.headerView reloadWithScrollView:scrollView]
 7、需要设置 self.automaticallyAdjustsScrollViewInsets = NO; 防止滚动视图有偏差

最后献上Demo地址:http://download.csdn.net/detail/hbblzjy/9597855



文/leiliang(简书作者)
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值