UITableView下拉放大效果
来源:互联网 发布:植美村面膜怎么样知乎 编辑:程序博客网 时间:2024/05/16 01:24
下拉放大效果非常常见, 做起来也比较简单, 主要思路如下:
- 需要下拉放大的图片最好是add进tableview的最底部,而不要作为tableview的头部
- 设置图片的y值为一个负数(我设置的是“-图片的高度”),这样图片的底部就是处于屏幕的顶部
- 设置tableview的内边距contentInset, 设置为图片高度的一半,这样图片就会露出一半
- 在UIScrollViewDelegate代理方法中计算下拉的偏移量, 根据偏移量设置图片缩放的比例, 也可以通过修改图片的宽高达到效果(设置图片的内容模式)
#import "ViewController.h"#define kScreenHeight [UIScreen mainScreen].bounds.size.heightfloat ratio = 3; // 阻尼系数CGFloat imageWidth = 415; // 图片宽度CGFloat imageHeight = 260; // 图片高度@interface ViewController () <UITableViewDataSource, UITableViewDelegate>@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) UIImageView *topImageView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; [self setupSubViews];}- (void)setupSubViews{ UIImageView *topImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"13.jpg"]]; topImageView.contentMode = UIViewContentModeScaleAspectFill; // 1.图片的Y值设置为“-图片高度” topImageView.frame = CGRectMake(0, -imageHeight, imageWidth, imageHeight); _topImageView = topImageView; // 2.把图片插入到TableView的底部 [self.tableView insertSubview:topImageView atIndex:0]; // 3.设置TableView的内边距为“图片高度的一半”, 也就是说默认图片会露出来一半 self.tableView.contentInset = UIEdgeInsetsMake(imageHeight / 2, 0, 0, 0); [self.view addSubview:_tableView];}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 30;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reuseId = @"reuseId"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];; } cell.textLabel.text = [NSString stringWithFormat:@"下拉有惊喜哦%ld", indexPath.row]; return cell;}#pragma mark - UIScrollViewDelegate(缩放)- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ // 4.计算出Y轴的偏移量, 默认的位置为“-图片高度”, 所以需要加上图片高度的一半 CGFloat offsetY = scrollView.contentOffset.y + imageHeight / 2; if (offsetY > 0) { return; } NSLog(@"offsetY = %f", offsetY); // 5.计算缩放比例 = 偏移量 / 屏幕高度 * 阻尼系数 (阻尼系数越大, 越难拖动) CGFloat scale = -offsetY / kScreenHeight * ratio; _topImageView.transform = CGAffineTransformMakeScale(1 + scale, 1 + scale);}#pragma mark - UIScrollViewDelegate(修改高度)//- (void)scrollViewDidScroll:(UIScrollView *)scrollView//{// // 4.计算出Y轴的偏移量, 默认的位置为“-图片高度”, 所以需要加上图片高度的一半// CGFloat offsetY = scrollView.contentOffset.y + imageHeight / 2;// if (offsetY > 0) {// return;// }// NSLog(@"offsetY = %f", offsetY);//// // 5. 修改高度达到效果// // 因为图片的contentModel已经设置为UIViewContentModeScaleAspectFill,所以只要修改高度, 宽度也会等比例伸缩// CGRect frame = _topImageView.frame;// frame.size.height = imageHeight - offsetY;// _topImageView.frame = frame;//}- (UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.rowHeight = 50; } return _tableView;}@end
效果如下:
0 0
- UITableView下拉放大效果
- 下拉UITableview实现图片放大效果
- UITableView下拉顶部图片放大效果
- UITableView头部ImageView下拉放大效果,导航栏透明渐变
- UITableView头部带有图片并且下拉图片放大效果
- UITableView透视图下拉放大.
- UITableView 下拉放大
- 下拉放大效果
- tableView 下拉放大效果
- [IOS]对UITableView的简单封装,实现了下拉放大头部图片的效果
- [iOS]实现UITableView头部带有图片并且下拉图片放大效果
- iOS仿keep5.3.0版本运动能力界面实现UITableView图片下拉放大效果
- ios下拉放大图片效果
- 顶部View 下拉放大效果
- iOS_下拉图片放大效果
- iOS 下拉放大头像效果
- 下拉放大及回弹效果
- 图片下拉缩小放大效果
- 442. Find All Duplicates in an Array
- request_firmware
- MQTT协议笔记之头部信息
- 4、网络根文件系统(NFS)
- ThinkPHP视图/关联模型使用记录
- UITableView下拉放大效果
- html总概述
- linux 挂载磁盘 mount
- Docker安装Elasticsearch的遇到的那些坑
- 集合框架_ArrayList存储字符串并遍历增强for版
- ListView的局部更新,不调用notify方法
- 5、字符设备驱动LEDS
- 前端--一个非前端人员的个人主页优化历程
- Android中pendingIntent的深入理解