新浪微博客户端(64)-下拉放大

 

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,weak) UIImageView *imageView;

@end

static CGFloat _imageViewWH;


@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
   
    _imageViewWH = self.tableView.frame.size.width;
    
    
    self.tableView.contentInset = UIEdgeInsetsMake(_imageViewWH * 0.5, 0, 0, 0);
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"biaoqingdi"];
    imageView.frame = CGRectMake(0, -(_imageViewWH), _imageViewWH, _imageViewWH);
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    [self.tableView insertSubview:imageView atIndex:0];
    self.imageView = imageView;
    
}



#pragma mark - TableView 代理方法

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

    return 20;

}


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

    static NSString *ID = @"pull";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
    return cell;

}



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



    // 计算当前移动的距离
    // 移动距离 = tableView初始移动距离 - scrollView移动时Y轴方向上的移动距离
    CGFloat moveDistance = -(_imageViewWH * 0.5) - scrollView.contentOffset.y;
    if (moveDistance < 0) return;
    CGRect imageViewF = self.imageView.frame;
    imageViewF.size.height = _imageViewWH + moveDistance * 1.5; // 乘以0.5可加快图片变形速度
    self.imageView.frame = imageViewF;
    
    NSLog(@"当前移动的距离为:%f",moveDistance);
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最终效果:

 

 

  

posted @ 2016-12-20 20:55  夜行过客  阅读(283)  评论(0编辑  收藏  举报