博客> 顶部图片吸顶拉伸缩放
顶部图片吸顶拉伸缩放
2019-11-10 05:05 评论:0 阅读:1090 qianqianzhang
ios 视图吸顶 图片拉伸缩放
  1. 由于设计的需求,我们在开发项目中会用到类似弹簧状态的图片拉伸效果,今天就和大家分享一个好用的吸顶拉伸图片效果。我是将顶部图片作为表的表头的情况下,对图片拉伸的。

1.设置表头视图

UIView *headerBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_OF_DETAIL_PICTURE)];

headerBackView.backgroundColor = COLOR_BACKGROUD_GRAY;
self.tableView.tableHeaderView = headerBackView;

self.headerImgaeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_OF_DETAIL_PICTURE)];

self.headerImgaeView.contentMode = UIViewContentModeScaleAspectFill;

self.headerImgaeView.clipsToBounds = YES;

[self.headerImgaeView sd_setImageWithURL:[NSURL

URLWithString:_carData.photo_cover]placeholderImage:[UIImage imageNamed:@"placeholder"]];

[headerBackView addSubview:self.headerImgaeView];

2.设置设置视图拉伸时的frame值改变视图拉伸状态

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

    CGFloat width = self.view.frame.size.width;

    CGFloat yOffset = scrollView.contentOffset.y;

    if (yOffset < 0){
    
        CGFloat totalOffset = HEIGHT_OF_DETAIL_PICTURE + ABS(yOffset);
    
        CGFloat f = totalOffset / HEIGHT_OF_DETAIL_PICTURE;
    
        self.headerImgaeView.frame = CGRectMake(-(width * f - width) / 2, yOffset, width * f, totalOffset);
    
    }else{

    //图片缩放

    CGFloat totalOffset = HEIGHT_OF_DETAIL_PICTURE - ABS(yOffset);
    
    CGFloat f = totalOffset / HEIGHT_OF_DETAIL_PICTURE;
    
    self.headerImgaeView.frame = CGRectMake(-(width * f - width) / 2, yOffset, width * f, totalOffset);

    } }

收藏
0
sina weixin mail 回到顶部