UITableView 头部图片 图片拉伸

UITableView上面添加一个大图片,拖动tableView,图片拉伸,停止拖动,图片变为原来尺寸

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    CGFloat offsetY = scrollView.contentOffset.y;

    CGFloat offsetH = imageBGHeight + offsetY;

    if (offsetH < 0) {

        CGRect frame = self.imageBG.frame;

        frame.size.height = imageBGHeight - offsetH;

        frame.origin.y = -imageBGHeight + offsetH;

        self.imageBG.frame = frame;

    }

// 此处是设置导航栏颜色的变化的    

    CGFloat alpha = offsetH / imageBGHeight;

    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor cyanColor] colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];

}

#pragma mark - 返回一张纯色图片

/** 返回一张纯色图片 */

- (UIImage *)imageWithColor:(UIColor *)color {

    // 描述矩形

    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

    // 开启位图上下文

    UIGraphicsBeginImageContext(rect.size);

    // 获取位图上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    // 使用color演示填充上下文

    CGContextSetFillColorWithColor(context, [color CGColor]);

    // 渲染上下文

    CGContextFillRect(context, rect);

    // 从上下文中获取图片

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

    // 结束上下文

    UIGraphicsEndImageContext();

    return theImage;

}

第二种方法  

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    CGFloat offsetY  = scrollView.contentOffset.y;

    if (offsetY < -KActivityDetailHeadH) {

        CGRect frame = _expandZoomImageView.frame;

        frame.origin.y = offsetY - 20;

        frame.size.height = -offsetY + 20;

        _expandZoomImageView.frame = frame;

    }

 KActivityDetailHeadH 此处是tableView上面图片的高度

1
原文地址:https://www.cnblogs.com/fantasy3588/p/5516572.html