下拉UITableview实现图片放大效果

下拉UITableview实现图片放大效果,在这里主要在scrollViewDidScroll:这个方法中根据偏移情况来修改图片的frame实现放大。

#import "ViewController.h"
#define IMAGEHEIGHT 260
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (strong , nonatomic) UITableView *tableView;
@property (strong , nonatomic) UIImageView *barImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"下拉图片放大";
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView addSubview:[self createImageView]];
    self.tableView.contentInset = UIEdgeInsetsMake(IMAGEHEIGHT, 0, 0, 0);
    [self.view addSubview:tableView]; 
}
- (UIImageView *)createImageView
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -IMAGEHEIGHT, self.tableView.frame.size.width, IMAGEHEIGHT)];
    imageView.image = [UIImage imageNamed:@"2"];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.tag = 111;
    return imageView;
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(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 == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"test%ld", indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
#pragma mark - scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    if (point.y < -IMAGEHEIGHT) {
        CGRect rect = [self.tableView viewWithTag:101].frame;
        rect.origin.y = point.y;
        rect.size.height = -point.y;
        [self.tableView viewWithTag:111].frame = rect;
    }
}

@end

猜你喜欢

转载自blog.csdn.net/qq_32796151/article/details/51802083