iOS【tableView的下拉放大图片的方法】


一:效果

tableview下拉的时候上部分图片放大会 

这里写图片描述

二:代码

直接上代码,自己研究吧


#import "NYViewController.h"

//图片的高度
const CGFloat NYTopViewH = 350;

@interface NYViewController ()
@property (nonatomic, weak) UIImageView *topView;
@end

@implementation NYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 设置内边距(让cell往下移动一段距离)
    self.tableView.contentInset = UIEdgeInsetsMake(NYTopViewH * 0.5, 0, 0, 0);

    UIImageView *topView = [[UIImageView alloc] init];
    topView.image = [UIImage imageNamed:@"biaoqingdi"];
    topView.frame = CGRectMake(0, -NYTopViewH, 320, NYTopViewH);

    //设置图片内容模式,让按照原来宽高比缩放
    topView.contentMode = UIViewContentModeScaleAspectFill;
    [self.tableView insertSubview:topView atIndex:0];
    self.topView = topView;
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"测试数据---%d", indexPath.row];

    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 向下拽了多少距离
    CGFloat down = -(NYTopViewH * 0.5) - scrollView.contentOffset.y;
    if (down < 0) return;

    CGRect frame = self.topView.frame;

    // 5决定图片变大的速度,值越大,速度越快
    frame.size.height = NYTopViewH + down * 5;
    self.topView.frame = frame;
}

@end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值