// // MOGuideMaskView.m // MiMoLive // // Created by MiMo on 2025/6/12. // #import "MOGuideMaskView.h" @interface MOGuideMaskView () @property (nonatomic, strong) NSMutableArray *holePaths; @end @implementation MOGuideMaskView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _holePaths = [NSMutableArray array]; _maskColor = [MOTools colorWithHexString:@"#000000" alpha:0.4]; _dismissOnTouch = YES; self.backgroundColor = UIColor.clearColor; self.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)]; [self addGestureRecognizer:tap]; } return self; } - (void)addHoleWithRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius { UIBezierPath *hole = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius]; [self.holePaths addObject:hole]; } - (void)show { UIBezierPath *fullPath = [UIBezierPath bezierPathWithRect:self.bounds]; for (UIBezierPath *hole in self.holePaths) { [fullPath appendPath:hole]; } fullPath.usesEvenOddFillRule = YES; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.path = fullPath.CGPath; maskLayer.fillRule = kCAFillRuleEvenOdd; self.backgroundColor = self.maskColor; self.layer.mask = maskLayer; [[UIApplication sharedApplication].keyWindow addSubview:self]; } - (void)showInView:(UIView *)superView { UIBezierPath *fullPath = [UIBezierPath bezierPathWithRect:self.bounds]; for (UIBezierPath *hole in self.holePaths) { [fullPath appendPath:hole]; } fullPath.usesEvenOddFillRule = YES; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.path = fullPath.CGPath; maskLayer.fillRule = kCAFillRuleEvenOdd; self.backgroundColor = self.maskColor; self.layer.mask = maskLayer; [superView addSubview:self]; } - (void)onTap { if (self.dismissOnTouch) { self.alpha = 1.0; [UIView animateWithDuration:0.3 animations:^{ self.alpha = 0.0; } completion:^(BOOL finished) { [self removeFromSuperview]; if (self.hideBlock) { self.hideBlock(); } }]; } } @end