iOS为高斯模糊UIEffectView添加模糊值

有时候UI给的图里面用到了模糊效果,但是官方的UIEffectView的模糊值太重,想要控制其模糊程度的话,就需要配合UIViewPropertyAnimator使用。

头文件CustomEffectView.h

@interface CustomEffectView : UIVisualEffectView

@property (nonatomic, assign) CGFloat intensity;

@property (nonatomic, strong) UIViewPropertyAnimator * animator;

- (instancetype)initWithEffect:(UIVisualEffect *)effect intensity:(CGFloat)intensity;

@end

CustomEffectView.m

@implementation CustomEffectView

- (instancetype)initWithEffect:(UIVisualEffect *)effect intensity:(CGFloat)intensity {
    self = [super initWithEffect:nil];
    if (self) {
        self.effect = nil;
        self.intensity = intensity;
        self.animator = [[UIViewPropertyAnimator alloc] initWithDuration:1 curve:UIViewAnimationCurveLinear animations:^{
            self.effect = effect;
        }];
        self.animator.fractionComplete = self.intensity;
        if (@available(iOS 11.0, *)) {
            self.animator.pausesOnCompletion = YES;
        }
    }
    return self;
}
@end

使用方法

使用initWithEffect:intensity:方法初始化,并在viewWillAppear方法内调用setEffectView

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容