我是靠谱客的博主 幽默哑铃,这篇文章主要介绍iOS tableView实现顶部图片拉伸效果,现在分享给大家,希望可以做个参考。

大家可能注意到一些app的tableView的顶部图片,会随着你拉伸而跟着拉伸变大,下面这是我的一些想法

原图:

效果图:

下面附上代码吧,这里的图片不是添加在tabview的header上

复制代码
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#define SCREEN_W [UIScreen mainScreen].bounds.size.width #define SCREEN_H [UIScreen mainScreen].bounds.size.height #define TOP 200 //顶部预留 #import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是幽默哑铃最近收集整理的关于iOS tableView实现顶部图片拉伸效果的全部内容,更多相关iOS内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(145)

评论列表共有 0 条评论

立即
投稿
返回
顶部