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

2016-09-30  本文已影响278人  小雨雨儿

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

- (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

上一篇下一篇

猜你喜欢

热点阅读