浅谈下拉放大

在这里,博主我用了比较简单的方法实现了下拉放大的功能,基本内容都在代码中有MARK,如果还有什么不明白的可以随时提问

因为考虑到查询这功能的都以新手居多,所以,代码实现的方法是最简单的很多细节都没有写上,而且方法也以让新手看懂为主,大神轻虐啊

#import "ViewController.h"

#define imageHeight 180.0f


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

{

    UITableView *_tab;

    UIImageView *_imageV;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    _tab = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

    _tab.delegate = self;

    _tab.dataSource = self;

    //给tableview设定边距

    _tab.contentInset = UIEdgeInsetsMake(imageHeight, 0, 0, 0);

    [self.view addSubview:_tab];


    //创建UIImageView

    _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, -imageHeight, self.view.frame.size.width, imageHeight)];//因为上面设置了边距所有这里的y坐标需要随之改变

    _imageV.backgroundColor = [UIColor yellowColor];

    _imageV.image = [UIImage imageNamed:@"aiqinhai.jpg"];


    //重点(不设置那将只会被纵向拉伸)

    //contentMode = UIViewContentModeScaleAspectFill时,高度改变宽度也跟着改变

    _imageV.contentMode = UIViewContentModeScaleAspectFill;

    

//    [_tab addSubview:_imageV];//要使用 addSubview 的话 设定的ImageView的高度必须等于或大于图片的高度

    [_tab insertSubview:_imageV atIndex:0];


}


#warning -mark 竖图不行

//代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    //计算CGFloat

    CGFloat y = scrollView.contentOffset.y+64;

    NSLog(@"%f",y);

    if (y<-imageHeight) {

        CGRect frame = _imageV.frame;

        frame.origin.y = y;

        frame.size.height = -y;

        _imageV.frame = frame;

    }

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

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

{

    return 10;

}


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

{

    static NSString *identifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];


    }

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

    return cell;

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值