iOS主流个人主页随滚动可缩放头图

效果是向上滚动图片变窄,向上滚动图片不变

自定义头部

#import <UIKit/UIKit.h>

@interface ZoomHeaderView : UIView

- (void)updateHeaderImageViewFrameWithOffsetY:(CGFloat)offsetY;

@end

#import "ZoomHeaderView.h"

@interface ZoomHeaderView ()

@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, assign) CGRect originalHeaderImageViewFrame;

@end

@implementation ZoomHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        headerImageView.clipsToBounds = YES;
        headerImageView.contentMode = UIViewContentModeScaleAspectFill;
        headerImageView.image = [UIImage imageNamed:@"head.png"];
        [self addSubview:headerImageView];
        self.headerImageView = headerImageView;
        self.originalHeaderImageViewFrame = self.bounds;
    }
    return self;
}

//根据offsetY更新布局
- (void)updateHeaderImageViewFrameWithOffsetY:(CGFloat)offsetY
{
    //防止height小于0
    if (self.originalHeaderImageViewFrame.size.height - offsetY < 0) {
        return;
    }
    //如果不使用约束的话,图片的y值要上移offsetY,同时height也要增加offsetY
    CGFloat x = self.originalHeaderImageViewFrame.origin.x;
    CGFloat y = self.originalHeaderImageViewFrame.origin.y + offsetY;
    CGFloat width = self.originalHeaderImageViewFrame.size.width;
    CGFloat height = self.originalHeaderImageViewFrame.size.height - offsetY;
    self.headerImageView.frame = CGRectMake(x, y, width, height);
}

@end

外部调用

#import "ViewController.h"
#import "ZoomHeaderView.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,strong) ZoomHeaderView *headerView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];

    ZoomHeaderView *headerView = [[ZoomHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
    tableView.tableHeaderView = headerView;
    self.headerView = headerView;
}

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

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

    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    [self.headerView updateHeaderImageViewFrameWithOffsetY:offsetY];
}

更详细在下面
转载自:http://www.jianshu.com/p/9c86eb8c1b76

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值