本篇文章為大家展示了怎么在iOS中使用tableView實現頂部圖片拉伸效果,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
#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實現頂部圖片拉伸效果,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。