最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
iOS tableView实现顶部图片拉伸效果
时间:2022-06-26 05:47:46 编辑:袖梨 来源:一聚教程网
大家可能注意到一些app的tableView的顶部图片,会随着你拉伸而跟着拉伸变大,下面这是我的一些想法
原图:
效果图:
下面附上代码吧,这里的图片不是添加在tabview的header上
#define SCREEN_W [UIScreen mainScreen].bounds.size.width #define SCREEN_H [UIScreen mainScreen].bounds.size.height #define TOP 200 //顶部预留 #import "ViewController.h" @interface ViewController ()@property (nonatomic,strong)UITableView *tableV; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self creatTableView]; } - (void)creatTableView { self.automaticallyAdjustsScrollViewInsets = NO; self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStylePlain]; self.tableV.contentInset = UIEdgeInsetsMake(TOP, 0, 0, 0); self.tableV.delegate = self; self.tableV.dataSource = self; //创建顶部图片 UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -TOP, SCREEN_W, TOP)]; imageView.tag = 1000; //更改图片显示模式 根据图片原有尺寸进行显示 将多余部分切除 imageView.contentMode = UIViewContentModeScaleAspectFill; //多余部分隐藏 imageView.clipsToBounds = YES; imageView.image = [UIImage imageNamed:@"pic"]; [self.view addSubview:_tableV]; [self.tableV addSubview:imageView]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { float offSet = scrollView.contentOffset.y; if (offSet < -200) { UIImageView * tempImageView = (UIImageView*)[self.view viewWithTag:1000]; CGRect f = tempImageView.frame; //保持图片原点仍为屏幕左上方 f.origin.y = offSet; //保证图片根据滑动高度拉伸 f.size.height = -offSet; //给图片重新设置坐标 tempImageView.frame = f; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cell = @"cell"; UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cell]; if (!myCell) { myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell]; } myCell.textLabel.text = @"我是 cell"; return myCell; } @end
相关文章
- 少年三国志2时空传怎么打 04-26
- 星穹铁道注销账号对原神有影响吗 04-26
- 忘仙巫皇怎么打造 04-26
- 求生之路2怎么杀witch 04-26
- qq飞车如何升级最快 04-26
- 星际战甲怎么把库伯带上 04-26