模糊主要使用UIVisualEffectView,这只在ios8以后适用

//模糊的遮罩view
@property(nonatomic,strong) UIVisualEffectView *effectView;
@property(nonatomic,strong) CollectionviewLayout *layout;

CollectionviewLayout *layout = [[CollectionviewLayout alloc]init];
layout.itemSize = CGSizeMake(mScreenWidth, 40);
layout.headerReferenceSize = CGSizeMake(mScreenWidth, collectionViewHeaderHeight);
self.layout = layout;

遮罩view的设置

UIBlurEffect *blur = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleLight)];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
effectView.alpha = 0;
self.effectView = effectView;
[_collectionView addSubview:_effectView];

header是UIImageView,大小为layout.headerReferenceSize
模糊是在-(void)scrollViewDidScroll:(UIScrollView *)scrollView中写

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSetY = scrollView.contentOffset.y;
    CGFloat deltaY = fabs(offSetY);
    if (offSetY < 0) {
        CGSize headerSize = [self.layout headerReferenceSize];
        CGRect headRect = CGRectZero;
        headRect.size.height = headerSize.height + deltaY;
        headRect.size.width = headerSize.width + deltaY;
        headRect.origin.y = headRect.origin.y - deltaY;
        headRect.origin.x = headRect.origin.x - deltaY/2;
        //UIVisualEffectView的frame应与header的ImageView的frame相同
        //上述headRect也为header的imageView的frame
        self.effectView.frame = headRect;
        self.effectView.alpha = deltaY/collectionViewHeaderHeight*1.5 ;
    }
}

效果为

posted on 2017-01-19 14:44  lixin327  阅读(1177)  评论(1编辑  收藏