MOGuideMaskView.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // MOGuideMaskView.m
  3. // MiMoLive
  4. //
  5. // Created by MiMo on 2025/6/12.
  6. //
  7. #import "MOGuideMaskView.h"
  8. @interface MOGuideMaskView ()
  9. @property (nonatomic, strong) NSMutableArray<UIBezierPath *> *holePaths;
  10. @end
  11. @implementation MOGuideMaskView
  12. - (instancetype)initWithFrame:(CGRect)frame {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. _holePaths = [NSMutableArray array];
  16. _maskColor = [MOTools colorWithHexString:@"#000000" alpha:0.4];
  17. _dismissOnTouch = YES;
  18. self.backgroundColor = UIColor.clearColor;
  19. self.userInteractionEnabled = YES;
  20. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)];
  21. [self addGestureRecognizer:tap];
  22. }
  23. return self;
  24. }
  25. - (void)addHoleWithRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius {
  26. UIBezierPath *hole = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
  27. [self.holePaths addObject:hole];
  28. }
  29. - (void)show {
  30. UIBezierPath *fullPath = [UIBezierPath bezierPathWithRect:self.bounds];
  31. for (UIBezierPath *hole in self.holePaths) {
  32. [fullPath appendPath:hole];
  33. }
  34. fullPath.usesEvenOddFillRule = YES;
  35. CAShapeLayer *maskLayer = [CAShapeLayer layer];
  36. maskLayer.path = fullPath.CGPath;
  37. maskLayer.fillRule = kCAFillRuleEvenOdd;
  38. self.backgroundColor = self.maskColor;
  39. self.layer.mask = maskLayer;
  40. [[UIApplication sharedApplication].keyWindow addSubview:self];
  41. }
  42. - (void)showInView:(UIView *)superView {
  43. UIBezierPath *fullPath = [UIBezierPath bezierPathWithRect:self.bounds];
  44. for (UIBezierPath *hole in self.holePaths) {
  45. [fullPath appendPath:hole];
  46. }
  47. fullPath.usesEvenOddFillRule = YES;
  48. CAShapeLayer *maskLayer = [CAShapeLayer layer];
  49. maskLayer.path = fullPath.CGPath;
  50. maskLayer.fillRule = kCAFillRuleEvenOdd;
  51. self.backgroundColor = self.maskColor;
  52. self.layer.mask = maskLayer;
  53. [superView addSubview:self];
  54. }
  55. - (void)onTap {
  56. if (self.dismissOnTouch) {
  57. self.alpha = 1.0;
  58. [UIView animateWithDuration:0.3 animations:^{
  59. self.alpha = 0.0;
  60. } completion:^(BOOL finished) {
  61. [self removeFromSuperview];
  62. if (self.hideBlock) {
  63. self.hideBlock();
  64. }
  65. }];
  66. }
  67. }
  68. @end