| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- //
- // MOGuideMaskView.m
- // MiMoLive
- //
- // Created by MiMo on 2025/6/12.
- //
- #import "MOGuideMaskView.h"
- @interface MOGuideMaskView ()
- @property (nonatomic, strong) NSMutableArray<UIBezierPath *> *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
|