图片下拉自动放大

本文详细介绍了如何在Objective-C中利用UITableView实现滚动视图,并将ImageView以特定方式集成到其中,包括设置内容模式、剪裁图片、调整内容偏移和高度变化,以实现在滚动过程中动态调整图片位置和大小的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "ViewController.h"

 

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView * tableVIew;

@property(nonatomic,strong)UIImageView * headImageView;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    _tableVIew = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

    

    _tableVIew.dataSource = self;

    

    _tableVIew.delegate = self;

    

    [self.view addSubview:_tableVIew];

    

    [_tableVIew registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ID"];

    

    //将ImageView以addSubView的方式加入TableView

    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, self.view.frame.size.width, 150)];

    

    _headImageView.image = [UIImage imageNamed:@"123.jpg"];

    

    //停靠模式

    //UIViewContentModeScaleAspectFill 无论坐标怎么变,图片比例一直保持不变

    _headImageView.contentMode = UIViewContentModeScaleAspectFill;

    

    //切除多余部分

    _headImageView.clipsToBounds = YES;

    [_tableVIew addSubview:_headImageView];

    

    //contentInset可以为继承于ScrollView的控件添或减少额外的滑动区域

    /*

     4个参数分别为相比原有坐标所需添加额外显示区域的偏移量

     */

    _tableVIew.contentInset = UIEdgeInsetsMake(150, 0, 0, 0);

    

    //contentInset和contentOffset为对应关系

    

   

}

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    return 10;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID" forIndexPath:indexPath];

    

    cell.textLabel.text = [NSString stringWithFormat:@"第%lu行",indexPath.row];

    

    return cell;

}

 

//滚动视图只要滑动即会被调用的方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    float offSet = scrollView.contentOffset.y;

    

    if (offSet < - 150) {

        //1.图片的顶点始终保持在屏幕的顶点

        //得到当前视图的坐标

        CGRect rect = _headImageView.frame;

        

        //-150-(-offSet-150)

        rect.origin.y = offSet;

        

        //2.更改图片高度

        rect.size.height = - offSet;

        

        _headImageView.frame = rect;

    }

    NSLog(@"%f",offSet);

}

转载于:https://www.cnblogs.com/block123/p/4919529.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值