IOS 列表头视图下拉放大

我也是看了一位大神的代码,感觉这个很巧妙,来写下笔记记一下。直接贴代码

tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    tableview.contentInset = UIEdgeInsetsMake(178.3, 0, 0, 0);
[self.view addSubview:tableview];

 bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0,-178.3, s.width, 178.3)];
 [tableview addSubview:bgimage];
上面就是表和视图的基本位置,这里很是巧妙,将视图放在tableview上,真正的列表又空出了视图的位置。重点是下面这个方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset = scrollView.contentOffset.y;
    NSLog(@"%f",yOffset);
    if (yOffset < -178.3) {
        CGRect f = bgimage.frame;
        f.origin.y = yOffset;
        f.size.height = -yOffset;
        bgimage.frame = f;
    }
}

在下拉时,去实时地改变视图的位置,以及大小,这样做很历害,就这几句代码就好了么?不是的,还有几句,来设置图片是否填充,要不然图片就只能随着自己拉伸而拉伸,不好看
//关键步骤 设置可变化背景view属性
        bgimage.autoresizingMask = UIViewAutoresizingFlexibleHeight| UIViewAutoresizingFlexibleWidth;
        bgimage.clipsToBounds = YES;
        bgimage.contentMode = UIViewContentModeScaleAspectFill;


posted @ 2015-01-26 11:11  赫凯  阅读(36)  评论(0)    收藏  举报