您的位置:首页 > 移动开发 > IOS开发

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

2016-06-29 14:08 537 查看
效果是向上滚动图片变窄,向上滚动图片不变

自定义头部

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  界面