iOS实现下拉放大的功能

#import "HMViewController.h"

const CGFloat ICITopViewH = 350;

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

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置内边距(让cell往下移动一段距离)
    self.tableView.contentInset = UIEdgeInsetsMake(ICITopViewH * 0.5, 0, 0, 0);
    
    UIImageView *topView = [[UIImageView alloc] init];
    topView.image = [UIImage imageNamed:@"biaoqingdi"];
    topView.frame = CGRectMake(0, -ICITopViewH, 320, ICITopViewH);
    //设置UUIImageView的内容显示模式。来实现下拉放大的关键
    topView.contentMode = UIViewContentModeScaleAspectFill;
    //插入到tableView的第一个子控件,使用addSubView的话,下拉会把底部的Cell遮挡。
    [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:@"测试数据---%ld", (long)indexPath.row];
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 向下拽了多少距离
    CGFloat down = -(ICITopViewH * 0.5) - scrollView.contentOffset.y;
    if (down < 0) return;
    
    CGRect frame = self.topView.frame;
    // 5决定图片变大的速度,值越大,速度越快
    frame.size.height = ICITopViewH + down * 5;
    self.topView.frame = frame;
}

@end

 

posted on 2015-11-18 22:17  Alwaysyouare  阅读(697)  评论(0编辑  收藏  举报

导航