美文网首页
parallaxView实现方式总结-个人中心背景效果

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

作者: 小雨雨儿 | 来源:发表于2016-09-30 17:17 被阅读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

    相关文章

      网友评论

          本文标题:parallaxView实现方式总结-个人中心背景效果

          本文链接:https://www.haomeiwen.com/subject/msfryttx.html