控制UIVisualEffectView的模糊度

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,441评论 4 61
  • 转载自:https://github.com/Tim9Liu9/TimLiu-iOS 目录 UI下拉刷新模糊效果A...
    袁俊亮技术博客阅读 12,131评论 9 105
  • 嗯哼嗯哼蹦擦擦~~~ 转载自:https://github.com/Tim9Liu9/TimLiu-iOS 目录 ...
    philiha阅读 5,180评论 0 6
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,845评论 0 4
  • 公元:2019年11月28日19时42分农历:二零一九年 十一月 初三日 戌时干支:己亥乙亥己巳甲戌当月节气:立冬...
    石放阅读 7,199评论 0 2

友情链接更多精彩内容