123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- //
- // HWDimmedView.m
- // HWPanModal
- //
- // Created by heath wang on 2019/4/26.
- //
- #import "HWDimmedView.h"
- #import "HWVisualEffectView.h"
- #import "HWBackgroundConfig.h"
- @interface HWDimmedView ()
- @property (nonatomic, strong) UIView *backgroundView;
- @property (nonatomic, strong) HWVisualEffectView *blurView;
- @property (nonatomic, strong) HWBackgroundConfig *backgroundConfig;
- @property (nonatomic, assign) CGFloat maxDimAlpha;
- @property (nonatomic, assign) CGFloat maxBlurRadius;
- @property (nonatomic, assign) CGFloat maxBlurTintAlpha;
- @property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
- @property (nonatomic, assign) BOOL isBlurMode;
- @end
- @implementation HWDimmedView
- - (instancetype)initWithDimAlpha:(CGFloat)dimAlpha blurRadius:(CGFloat)blurRadius {
- self = [super initWithFrame:CGRectZero];
- if (self) {
- _maxBlurRadius = blurRadius;
- _maxDimAlpha = dimAlpha;
- [self commonInit];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- _maxDimAlpha = 0.7;
- [self commonInit];
- }
- return self;
- }
- - (instancetype)initWithBackgroundConfig:(HWBackgroundConfig *)backgroundConfig {
- self = [super initWithFrame:CGRectZero];
- if (self) {
- self.backgroundConfig = backgroundConfig;
- _maxDimAlpha = backgroundConfig.backgroundAlpha;
- _maxBlurRadius = backgroundConfig.backgroundBlurRadius;
- _blurTintColor = backgroundConfig.blurTintColor;
- [self commonInit];
- }
- return self;
- }
- - (void)commonInit {
- _dimState = DimStateOff;
- _maxBlurTintAlpha = 0.5;
- // default, max alpha.
- _percent = 1;
- _tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapView)];
- [self addGestureRecognizer:_tapGestureRecognizer];
- [self setupView];
- }
- - (void)setupView {
- self.isBlurMode = self.maxBlurRadius > 0 || self.backgroundConfig.visualEffect;
- if (self.isBlurMode) {
- [self addSubview:self.blurView];
- [self configBlurView];
- } else {
- [self addSubview:self.backgroundView];
- }
- }
- #pragma mark - layout
- - (void)layoutSubviews {
- [super layoutSubviews];
- // not call getter.
- _blurView.frame = self.bounds;
- _backgroundView.frame = self.bounds;
- }
- #pragma mark - touch action
- - (void)didTapView {
- self.tapBlock ? self.tapBlock(self.tapGestureRecognizer) : nil;
- }
- #pragma mark - public method
- - (void)reloadConfig:(HWBackgroundConfig *)backgroundConfig {
-
- for (UIView *view in self.subviews) {
- [view removeFromSuperview];
- }
-
- self.backgroundConfig = backgroundConfig;
- _maxDimAlpha = backgroundConfig.backgroundAlpha;
- _maxBlurRadius = backgroundConfig.backgroundBlurRadius;
- _blurTintColor = backgroundConfig.blurTintColor;
- [self setupView];
-
- DimState state = self.dimState;
- self.dimState = state;
- }
- #pragma mark - private method
- - (void)updateAlpha {
- CGFloat alpha = 0;
- CGFloat blurRadius = 0;
- CGFloat blurTintAlpha = 0;
- switch (self.dimState) {
- case DimStateMax:{
- alpha = self.maxDimAlpha;
- blurRadius = self.maxBlurRadius;
- blurTintAlpha = self.maxBlurTintAlpha;
- }
- break;
- case DimStatePercent: {
- CGFloat percent = MAX(0, MIN(1.0f, self.percent));
- alpha = self.maxDimAlpha * percent;
- blurRadius = self.maxBlurRadius * percent;
- blurTintAlpha = self.maxBlurTintAlpha * percent;
- }
- default:
- break;
- }
- if (self.isBlurMode) {
- if (self.backgroundConfig.visualEffect) return;
- self.blurView.blurRadius = blurRadius;
- self.blurView.colorTintAlpha = blurTintAlpha;
- } else {
- self.backgroundView.alpha = alpha;
- }
- }
- - (void)configBlurView {
- if (self.backgroundConfig.visualEffect) {
- [_blurView updateBlurEffect:self.backgroundConfig.visualEffect];
- } else {
- _blurView.colorTint = [UIColor whiteColor];
- _blurView.colorTintAlpha = self.maxBlurTintAlpha;
- _blurView.userInteractionEnabled = NO;
- }
- }
- #pragma mark - Setter
- - (void)setDimState:(DimState)dimState {
- _dimState = dimState;
- [self updateAlpha];
- }
- - (void)setPercent:(CGFloat)percent {
- _percent = percent;
- [self updateAlpha];
- }
- #pragma mark - Getter
- - (UIView *)backgroundView {
- if (!_backgroundView) {
- _backgroundView = [UIView new];
- _backgroundView.userInteractionEnabled = NO;
- _backgroundView.alpha = 0;
- _backgroundView.backgroundColor = [UIColor blackColor];
- }
- return _backgroundView;
- }
- - (HWVisualEffectView *)blurView {
- if (!_blurView) {
- _blurView = [HWVisualEffectView new];
- }
- return _blurView;
- }
- #pragma mark - Setter
- - (void)setBlurTintColor:(UIColor *)blurTintColor {
- _blurTintColor = blurTintColor;
- _blurView.colorTint = _blurTintColor;
- }
- @end
|