iOS开发-UITableView顶部图片下拉放大
关于顶部图片下拉放大,在用户展示的个人中心显示用户个人头像信息,设置UITableView的headerView实现,UITableView继承自UIScrollView,同样的设置UIScrollView的顶部图片也可以实现同样的效果,简单看一下实现的效果:
控制器中设置需要的属性变量:
1 2 3 4 | @property (strong,nonatomic) UITableView *tableView; @property (strong,nonatomic) NSArray *data; @property (strong,nonatomic) UIView *tableHeaderView; @property (strong,nonatomic) UIImageView *imageView; |
初始化属性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | -(UITableView *)tableView{ if (!_tableView) { _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0,SCREENWIDTH, SCREENHEIGHT)]; _tableView. delegate =self; _tableView.dataSource=self; _tableView.showsVerticalScrollIndicator=NO; _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [_tableView registerClass:[UITableViewCell class ] forCellReuseIdentifier:CellIdetifier]; } return _tableView; } -(UIView *)tableHeaderView{ if (!_tableHeaderView) { _tableHeaderView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,SCREENWIDTH, 100)]; } return _tableHeaderView; } -(UIImageView *)imageView{ if (!_imageView) { _imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 100)]; _imageView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; _imageView.clipsToBounds=YES; _imageView.contentMode=UIViewContentModeScaleAspectFill; } return _imageView; } |
UITableViewDelegate实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 | -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.data count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdetifier]; [cell.textLabel setText:self.data[indexPath.row]]; return cell; } |
ViewDidLoad中初始化imageView:
1 2 3 4 5 6 | self.data=@[ @"FlyElephant" , @"博客园" , @"UITableView图片放大" , @"http://www.cnblogs.com/xiaofeixiang/" ]; self.imageView.image=[UIImage imageNamed: @"Girl" ]; self.imageView.contentMode=UIViewContentModeScaleAspectFill; [self.tableHeaderView addSubview:self.imageView]; self.tableView.tableHeaderView=self.tableHeaderView; [self.view addSubview:self.tableView]; |
在UITableViewView向下拉动的过程中,改变imageView的位置:
1 2 3 4 5 6 7 8 9 10 | -( void )scrollViewDidScroll:(UIScrollView *)scrollView{ CGPoint offset = scrollView.contentOffset; if (offset.y < 0) { CGRect rect =self.tableHeaderView.frame; rect.origin.y = offset.y; rect.size.height =CGRectGetHeight(rect)-offset.y; self.imageView.frame = rect; self.tableHeaderView.clipsToBounds=NO; } } |
实现起来比较简单,希望对有需要的人有所帮助~
作者:FlyElephant
出处:http://www.cnblogs.com/xiaofeixiang
说明:博客经个人辛苦努力所得,如有转载会特别申明,博客不求技惊四座,但求与有缘人分享个人学习知识,生活学习提高之用,博客所有权归本人和博客园所有,如有转载请在显著位置给出博文链接和作者姓名,否则本人将付诸法律。
出处:http://www.cnblogs.com/xiaofeixiang
说明:博客经个人辛苦努力所得,如有转载会特别申明,博客不求技惊四座,但求与有缘人分享个人学习知识,生活学习提高之用,博客所有权归本人和博客园所有,如有转载请在显著位置给出博文链接和作者姓名,否则本人将付诸法律。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
2015-01-14 iOS开发-UI基础Demo