HWDimmedView.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. @interface HWDimmedView ()
  10. @property (nonatomic, strong) UIView *backgroundView;
  11. @property (nonatomic, strong) HWVisualEffectView *blurView;
  12. @property (nonatomic, assign) CGFloat maxDimAlpha;
  13. @property (nonatomic, assign) CGFloat maxBlurRadius;
  14. @property (nonatomic, assign) CGFloat maxBlurTintAlpha;
  15. @property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
  16. @property (nonatomic, assign) BOOL isBlurMode;
  17. @end
  18. @implementation HWDimmedView
  19. - (instancetype)initWithDimAlpha:(CGFloat)dimAlpha blurRadius:(CGFloat)blurRadius {
  20. self = [super initWithFrame:CGRectZero];
  21. if (self) {
  22. _maxBlurRadius = blurRadius;
  23. _maxDimAlpha = dimAlpha;
  24. [self commonInit];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithFrame:(CGRect)frame {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. _maxDimAlpha = 0.7;
  32. [self commonInit];
  33. }
  34. return self;
  35. }
  36. - (void)commonInit {
  37. _dimState = DimStateOff;
  38. _maxBlurTintAlpha = 0.5;
  39. // default, max alpha.
  40. _percent = 1;
  41. _tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapView)];
  42. [self addGestureRecognizer:_tapGestureRecognizer];
  43. [self setupView];
  44. }
  45. - (void)setupView {
  46. self.isBlurMode = self.maxBlurRadius > 0;
  47. if (self.isBlurMode) {
  48. [self addSubview:self.blurView];
  49. } else {
  50. [self addSubview:self.backgroundView];
  51. }
  52. }
  53. #pragma mark - layout
  54. - (void)layoutSubviews {
  55. [super layoutSubviews];
  56. // not call getter.
  57. _blurView.frame = self.bounds;
  58. _backgroundView.frame = self.bounds;
  59. }
  60. #pragma mark - touch action
  61. - (void)didTapView {
  62. self.tapBlock ? self.tapBlock(self.tapGestureRecognizer) : nil;
  63. }
  64. #pragma mark - private method
  65. - (void)updateAlpha {
  66. CGFloat alpha = 0;
  67. CGFloat blurRadius = 0;
  68. CGFloat blurTintAlpha = 0;
  69. switch (self.dimState) {
  70. case DimStateMax:{
  71. alpha = self.maxDimAlpha;
  72. blurRadius = self.maxBlurRadius;
  73. blurTintAlpha = self.maxBlurTintAlpha;
  74. }
  75. break;
  76. case DimStatePercent: {
  77. CGFloat percent = MAX(0, MIN(1.0f, self.percent));
  78. alpha = self.maxDimAlpha * percent;
  79. blurRadius = self.maxBlurRadius * percent;
  80. blurTintAlpha = self.maxBlurTintAlpha * percent;
  81. }
  82. default:
  83. break;
  84. }
  85. if (self.isBlurMode) {
  86. self.blurView.blurRadius = blurRadius;
  87. self.blurView.colorTintAlpha = blurTintAlpha;
  88. } else {
  89. self.backgroundView.alpha = alpha;
  90. }
  91. }
  92. #pragma mark - Setter
  93. - (void)setDimState:(DimState)dimState {
  94. _dimState = dimState;
  95. [self updateAlpha];
  96. }
  97. - (void)setPercent:(CGFloat)percent {
  98. _percent = percent;
  99. [self updateAlpha];
  100. }
  101. #pragma mark - Getter
  102. - (UIView *)backgroundView {
  103. if (!_backgroundView) {
  104. _backgroundView = [UIView new];
  105. _backgroundView.userInteractionEnabled = NO;
  106. _backgroundView.alpha = 0;
  107. _backgroundView.backgroundColor = [UIColor blackColor];
  108. }
  109. return _backgroundView;
  110. }
  111. - (HWVisualEffectView *)blurView {
  112. if (!_blurView) {
  113. _blurView = [HWVisualEffectView new];
  114. _blurView.colorTint = [UIColor whiteColor];
  115. _blurView.colorTintAlpha = self.maxBlurTintAlpha;
  116. _blurView.userInteractionEnabled = NO;
  117. }
  118. return _blurView;
  119. }
  120. #pragma mark - Setter
  121. - (void)setBlurTintColor:(UIColor *)blurTintColor {
  122. _blurTintColor = blurTintColor;
  123. _blurView.colorTint = _blurTintColor;
  124. }
  125. @end