iOS主流个人主页随滚动可缩放头图
2016-06-29 14:08
537 查看
效果是向上滚动图片变窄,向上滚动图片不变
自定义头部
外部调用
更详细在下面
转载自:http://www.jianshu.com/p/9c86eb8c1b76
自定义头部
#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
相关文章推荐
- winform异型不规则界面设计的实现方法
- 在ASP.NET 2.0中操作数据之二十:定制数据修改界面
- Android中设置只有程序第一次运行才显示的界面实现思路
- hta 实现的五子棋界面
- android开发之欢迎界面的小例子
- C#中载入界面的常用方法
- Android绘制炫酷引导界面
- android编程实现局部界面动态切换的方法
- Android判断现在所处界面是否为home主桌面的方法
- Android用户注册界面
- Android编程实现泡泡聊天界面实例详解(附源码)
- android 引导界面的实现方法
- VC 界面库皮肤库相关信息珍藏
- 在ASP.NET 2.0中操作数据之二十:定制数据修改界面
- Android中界面实现全屏显示的两种方式
- 简单做出不丑设计
- extmail 界面修改
- QT程序启动界面的使用
- The Book of Qt 4 翻译: 2.2 界面和处理逻辑的分离