parallaxView实现方式总结-个人中心背景效果

方法一:不对显示的图片的尺寸做特殊处理

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView != self.tableView) {
        return;
    }

    CGFloat coverOriginalHeight = CGRectGetHeight(self.converImageView.superview.frame);

    if (scrollView.contentOffset.y < 0) {
        CGFloat scale = (fabs(scrollView.contentOffset.y) + coverOriginalHeight) / coverOriginalHeight;

        CATransform3D transformScale3D = CATransform3DMakeScale(scale, scale, 1.0);
        CATransform3D transformTranslate3D = CATransform3DMakeTranslation(0, scrollView.contentOffset.y/2, 0);
        self.converImageView.layer.transform = CATransform3DConcat(transformScale3D, transformTranslate3D);

        UIEdgeInsets scrollIndicatorInsets = scrollView.scrollIndicatorInsets;
        scrollIndicatorInsets.top = fabs(scrollView.contentOffset.y) + coverOriginalHeight;
        scrollView.scrollIndicatorInsets = scrollIndicatorInsets;
    }
    else {
        self.converImageView.layer.transform = CATransform3DIdentity;

        if (scrollView.scrollIndicatorInsets.top != coverOriginalHeight) {
            UIEdgeInsets scrollIndicatorInsets = scrollView.scrollIndicatorInsets;
            scrollIndicatorInsets.top = coverOriginalHeight;
            scrollView.scrollIndicatorInsets = scrollIndicatorInsets;
        }
    }
}

方法二:设置背景 图片为tableView的headerView,
'- (void)scrollViewDidScroll:(UIScrollView *)scrollView '中改变ImgView的frame.headerView的clipsToBounds 设为NO

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
    [self.headerView layoutSubViewWhenScroll:offsetY];
}

- (void)layoutSubViewWhenScroll:(CGFloat) offsetY {  
    CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    rect.origin.y += offsetY;
    rect.size.height -= offsetY;
    self.bgImgView.frame = rect;
}

方法三:修改tableView的contentInset在'- (void)scrollViewDidScroll:(UIScrollView *)scrollView 中随时改变contentInset

详细的代码见:https://github.com/xinyuly/XYUserCenterAnimation

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容