HWDimmedView.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // HWDimmedView.m
  3. // HWPanModal
  4. //
  5. // Created by heath wang on 2019/4/26.
  6. //
  7. #import "HWDimmedView.h"
  8. #import "HWVisualEffectView.h"
  9. #import "HWBackgroundConfig.h"
  10. @interface HWDimmedView ()
  11. @property (nonatomic, strong) UIView *backgroundView;
  12. @property (nonatomic, strong) HWVisualEffectView *blurView;
  13. @property (nonatomic, strong) HWBackgroundConfig *backgroundConfig;
  14. @property (nonatomic, assign) CGFloat maxDimAlpha;
  15. @property (nonatomic, assign) CGFloat maxBlurRadius;
  16. @property (nonatomic, assign) CGFloat maxBlurTintAlpha;
  17. @property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
  18. @property (nonatomic, assign) BOOL isBlurMode;
  19. @end
  20. @implementation HWDimmedView
  21. - (instancetype)initWithDimAlpha:(CGFloat)dimAlpha blurRadius:(CGFloat)blurRadius {
  22. self = [super initWithFrame:CGRectZero];
  23. if (self) {
  24. _maxBlurRadius = blurRadius;
  25. _maxDimAlpha = dimAlpha;
  26. [self commonInit];
  27. }
  28. return self;
  29. }
  30. - (instancetype)initWithFrame:(CGRect)frame {
  31. self = [super initWithFrame:frame];
  32. if (self) {
  33. _maxDimAlpha = 0.7;
  34. [self commonInit];
  35. }
  36. return self;
  37. }
  38. - (instancetype)initWithBackgroundConfig:(HWBackgroundConfig *)backgroundConfig {
  39. self = [super initWithFrame:CGRectZero];
  40. if (self) {
  41. self.backgroundConfig = backgroundConfig;
  42. _maxDimAlpha = backgroundConfig.backgroundAlpha;
  43. _maxBlurRadius = backgroundConfig.backgroundBlurRadius;
  44. _blurTintColor = backgroundConfig.blurTintColor;
  45. [self commonInit];
  46. }
  47. return self;
  48. }
  49. - (void)commonInit {
  50. _dimState = DimStateOff;
  51. _maxBlurTintAlpha = 0.5;
  52. // default, max alpha.
  53. _percent = 1;
  54. _tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapView)];
  55. [self addGestureRecognizer:_tapGestureRecognizer];
  56. [self setupView];
  57. }
  58. - (void)setupView {
  59. self.isBlurMode = self.maxBlurRadius > 0 || self.backgroundConfig.visualEffect;
  60. if (self.isBlurMode) {
  61. [self addSubview:self.blurView];
  62. [self configBlurView];
  63. } else {
  64. [self addSubview:self.backgroundView];
  65. }
  66. }
  67. #pragma mark - layout
  68. - (void)layoutSubviews {
  69. [super layoutSubviews];
  70. // not call getter.
  71. _blurView.frame = self.bounds;
  72. _backgroundView.frame = self.bounds;
  73. }
  74. #pragma mark - touch action
  75. - (void)didTapView {
  76. self.tapBlock ? self.tapBlock(self.tapGestureRecognizer) : nil;
  77. }
  78. #pragma mark - public method
  79. - (void)reloadConfig:(HWBackgroundConfig *)backgroundConfig {
  80. for (UIView *view in self.subviews) {
  81. [view removeFromSuperview];
  82. }
  83. self.backgroundConfig = backgroundConfig;
  84. _maxDimAlpha = backgroundConfig.backgroundAlpha;
  85. _maxBlurRadius = backgroundConfig.backgroundBlurRadius;
  86. _blurTintColor = backgroundConfig.blurTintColor;
  87. [self setupView];
  88. DimState state = self.dimState;
  89. self.dimState = state;
  90. }
  91. #pragma mark - private method
  92. - (void)updateAlpha {
  93. CGFloat alpha = 0;
  94. CGFloat blurRadius = 0;
  95. CGFloat blurTintAlpha = 0;
  96. switch (self.dimState) {
  97. case DimStateMax:{
  98. alpha = self.maxDimAlpha;
  99. blurRadius = self.maxBlurRadius;
  100. blurTintAlpha = self.maxBlurTintAlpha;
  101. }
  102. break;
  103. case DimStatePercent: {
  104. CGFloat percent = MAX(0, MIN(1.0f, self.percent));
  105. alpha = self.maxDimAlpha * percent;
  106. blurRadius = self.maxBlurRadius * percent;
  107. blurTintAlpha = self.maxBlurTintAlpha * percent;
  108. }
  109. default:
  110. break;
  111. }
  112. if (self.isBlurMode) {
  113. if (self.backgroundConfig.visualEffect) return;
  114. self.blurView.blurRadius = blurRadius;
  115. self.blurView.colorTintAlpha = blurTintAlpha;
  116. } else {
  117. self.backgroundView.alpha = alpha;
  118. }
  119. }
  120. - (void)configBlurView {
  121. if (self.backgroundConfig.visualEffect) {
  122. [_blurView updateBlurEffect:self.backgroundConfig.visualEffect];
  123. } else {
  124. _blurView.colorTint = [UIColor whiteColor];
  125. _blurView.colorTintAlpha = self.maxBlurTintAlpha;
  126. _blurView.userInteractionEnabled = NO;
  127. }
  128. }
  129. #pragma mark - Setter
  130. - (void)setDimState:(DimState)dimState {
  131. _dimState = dimState;
  132. [self updateAlpha];
  133. }
  134. - (void)setPercent:(CGFloat)percent {
  135. _percent = percent;
  136. [self updateAlpha];
  137. }
  138. #pragma mark - Getter
  139. - (UIView *)backgroundView {
  140. if (!_backgroundView) {
  141. _backgroundView = [UIView new];
  142. _backgroundView.userInteractionEnabled = NO;
  143. _backgroundView.alpha = 0;
  144. _backgroundView.backgroundColor = [UIColor blackColor];
  145. }
  146. return _backgroundView;
  147. }
  148. - (HWVisualEffectView *)blurView {
  149. if (!_blurView) {
  150. _blurView = [HWVisualEffectView new];
  151. }
  152. return _blurView;
  153. }
  154. #pragma mark - Setter
  155. - (void)setBlurTintColor:(UIColor *)blurTintColor {
  156. _blurTintColor = blurTintColor;
  157. _blurView.colorTint = _blurTintColor;
  158. }
  159. @end