控制UIVisualEffectView的模糊度
2022-03-16 本文已影响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