UITableView拉伸效果

  1. 创建一个UITableView 和一个UIImageView

    @property (nonatomic, strong) UITableView *tableView;

    @property (nonatomic, strong) UIImageView *headerImgaeView;

  2. 初始化
     1 - (void)viewDidLoad {
     2     [super viewDidLoad];
     3 
     4     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStyleGrouped];
     5     self.tableView.delegate = self;
     6     self.tableView.dataSource = self;
     7     [self.view addSubview:self.tableView];
     8     
     9     
    10     [self layoutHeaderImageView];
    11 }
    viewDidLoad 
  3. layoutHeaderImageView

     1 - (void)layoutHeaderImageView
     2 {
     3     UIView *headerBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
     4     headerBackView.backgroundColor = [UIColor lightGrayColor];
     5     self.tableView.tableHeaderView = headerBackView;
     6     
     7     self.headerImgaeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
     8     self.headerImgaeView.backgroundColor = [UIColor greenColor];
     9     self.headerImgaeView.image = [UIImage imageNamed:@"beautiful.jpg"];
    10     self.headerImgaeView.contentMode = UIViewContentModeScaleAspectFill;
    11     self.headerImgaeView.clipsToBounds = YES;
    12     [headerBackView addSubview:self.headerImgaeView];
    13 
    14 }
    layoutHeaderImageView
  4. 滚动偏移的主要方法(这个为UIScrollViewDelegate中的代理方法)

     1 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
     2 {
     3     CGFloat width = self.view.frame.size.width;// 图片宽度
     4     CGFloat yOffset = scrollView.contentOffset.y; //偏移量
     5     
     6     NSLog(@":  %.2f", yOffset);
     7     
     8     if (yOffset < 0) {
     9         CGFloat totalOffset = 150 + ABS(yOffset);
    10         CGFloat f = totalOffset / 150; //缩放系数
    11         
    12         self.headerImgaeView.frame = CGRectMake(-(width * f - width) / 2, yOffset, width * f, totalOffset); //拉伸后的frame是同比例缩放
    13     }
    14     
    15     if (yOffset > 0) {
    16         CGFloat totalOffset = 150 - ABS(yOffset);
    17         CGFloat f = totalOffset / 150;
    18         
    19         self.headerImgaeView.frame = CGRectMake(-(width * f - width) / 2, yOffset, width * f, totalOffset);
    20     }
    21 }
    scrollView
原文地址:https://www.cnblogs.com/baidaye/p/5200215.html