美文网首页
控制UIVisualEffectView的模糊度

控制UIVisualEffectView的模糊度

作者: 大浪捉鱼 | 来源:发表于2022-03-16 14:33 被阅读0次

ZYVisualEffectView.h

@interface ZYVisualEffectView : UIVisualEffectView

/**
 @brief 可调整模糊度的UIVisualEffectView
 @param effect:visual effect, eg UIBlurEffect(style: .dark)
 @param intensity:custom intensity from 0.0 (no effect) to 1.0 (full effect) using linear scale
 */
- (instancetype)initWithEffect:(UIVisualEffect *)effect intensity:(CGFloat)intensity;

@end

ZYVisualEffectView.m

#import "ZYVisualEffectView.h"
#import <UIKit/UIVisualEffect.h>

@interface ZYVisualEffectView ()
@property (nonatomic, strong) UIVisualEffect *theEffect;
@property (nonatomic, assign) CGFloat customIntensity;
@property (nonatomic, strong) UIViewPropertyAnimator *animator;
@end

@implementation ZYVisualEffectView

- (instancetype)initWithEffect:(UIVisualEffect *)effect intensity:(CGFloat)intensity{
    self = [super initWithEffect:nil];
    if (self) {
        self.theEffect = effect;
        self.customIntensity = intensity;
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];
    
    self.effect = nil;
    if (self.animator) {
        [self.animator stopAnimation:YES];
    }
    self.animator = [[UIViewPropertyAnimator alloc] initWithDuration:1 curve:UIViewAnimationCurveLinear animations:^{
        self.effect = self.theEffect;
    }];
    self.animator.fractionComplete = self.customIntensity;
}

- (void)dealloc {
    [self.animator stopAnimation:YES];
}

@end

使用方法

self.effectView = [[ZYVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark] intensity:0.3];
    [self.coverIv addSubview:self.effectView];
    [self.effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.coverIv);
    }];

在封面图coverIv上面盖一层毛玻璃效果,并且可以通过intensity调整模糊程度

补充方法:用UIImage+ImageEffects.h 直接处理图片,不过在列表中使用此方法会有性能问题;

参考资料
https://www.itranslater.com/qa/details/2582527542427124736

https://www.jianshu.com/p/7f10e8348894

https://blog.csdn.net/Devin_Zhan/article/details/50135301

相关文章

网友评论

      本文标题:控制UIVisualEffectView的模糊度

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