iOS开发-tableView顶部图片拉伸

大家可能注意到一些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 ()<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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值