| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- //
- // KSGuideMaskView.m
- // MusicGradeExam
- //
- // Created by Kyle on 2020/7/28.
- // Copyright © 2020 DayaMusic. All rights reserved.
- //
- #import "KSGuideMaskView.h"
- NSInteger countNum = 0;
- @interface KSGuideMaskView ()
- /// 图层
- @property (nonatomic, weak) CAShapeLayer *fillLayer;
- /// 路径
- @property (nonatomic, strong) UIBezierPath *overlayPath;
- /// 透明区数组
- @property (nonatomic, strong) NSMutableArray *transparentPaths;
- /// 点击计数
- @property (nonatomic, assign) NSInteger index;
- /// tips数组
- @property (nonatomic, strong) NSMutableArray *tipsArray;
- @property (nonatomic, assign) NSInteger shaperLayerIndex;
- @property (nonatomic, strong) CAShapeLayer *shapeLayer;
- @end
- @implementation KSGuideMaskView
- - (instancetype)initWithFrame:(CGRect)frame {
-
- self = [super initWithFrame: [UIScreen mainScreen].bounds];
- if (self) {
- [self setUp];
- }
- return self;
- }
- - (void)setUp {
- self.index = 0;
-
- self.backgroundColor = [UIColor clearColor];
-
- UIColor *maskColor = [UIColor colorWithWhite:0 alpha:0.61];
- self.fillLayer.path = self.overlayPath.CGPath;
- self.fillLayer.fillRule = kCAFillRuleEvenOdd;
- self.fillLayer.fillColor = maskColor.CGColor;
-
- UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClickedMaskView)];
- [self addGestureRecognizer:tapGesture];
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- }
- - (void)addTips:(NSArray *)tipsArray transparentRect:(NSArray *)rectArray shaperLayerIndex:(NSInteger)index {
- if (tipsArray.count != rectArray.count) {
- return;
- }
- self.shaperLayerIndex = index;
- self.tipsArray = [NSMutableArray arrayWithArray:tipsArray];
- self.transparentPaths = [NSMutableArray arrayWithArray:rectArray];
- UIBezierPath *path = _transparentPaths[0];
- [self addTips:_tipsArray[0] withFrame:path.bounds];
- [self addTransparentPath:_transparentPaths[0]];
-
- }
- - (void)tapClickedMaskView {
-
- _index++;
-
- if (_index < _tipsArray.count) {
-
- [self refreshMask];
- [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
-
- UIBezierPath *path = _transparentPaths[_index];
- [self addTransparentPath:_transparentPaths[_index]];
- [self addTips:_tipsArray[_index] withFrame:path.bounds];
- } else {
- countNum = 0;
- [self dismissMaskView];
- }
- }
- - (void)addTips:(NSString *)tipsMessage withFrame:(CGRect)frame {
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
- label.text = tipsMessage;
- label.textAlignment = NSTextAlignmentCenter;
- label.font = [UIFont systemFontOfSize:15.0f];
- label.textColor = HexRGB(0xffffff);
- [self addSubview:label];
- [label mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.mas_equalTo(self);
- make.top.mas_equalTo(CGRectGetMaxY(frame) + 20);
- }];
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.layer.borderWidth = 1.0f;
- button.layer.borderColor = HexRGB(0xffffff).CGColor;
- button.layer.cornerRadius = 20.0f;
- [button setTitle:@"知道啦" forState:UIControlStateNormal];
- button.userInteractionEnabled = NO;
- // [button addTarget:self action:@selector(tapClickedMaskView) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:button];
- [button mas_makeConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(110);
- make.height.mas_equalTo(40);
- make.top.mas_equalTo(label.mas_bottom).offset(28);
- make.centerX.mas_equalTo(self);
- }];
- }
- - (void)addTransparentPath:(UIBezierPath *)transparentPath {
-
- [self.overlayPath appendPath:transparentPath];
- self.fillLayer.path = self.overlayPath.CGPath;
-
- if (_index == self.shaperLayerIndex) {
- CGRect frame = transparentPath.bounds;
- frame.size.width += 20;
- frame.size.height += 20;
- frame.origin.x -= 10;
- frame.origin.y -= 10;
- UIBezierPath *newPath = [UIBezierPath bezierPathWithOvalInRect:frame];
- _shapeLayer = [CAShapeLayer layer];
- _shapeLayer.frame = self.bounds;
- _shapeLayer.path = newPath.CGPath;
- _shapeLayer.lineWidth = 2.0f;
- _shapeLayer.strokeColor = HexRGB(0xffffff).CGColor;
- _shapeLayer.fillColor = [UIColor clearColor].CGColor;
- _shapeLayer.lineDashPattern = @[@(10), @(10)];
- [self.layer addSublayer:_shapeLayer];
- }
- else {
- if (_shapeLayer) {
- [_shapeLayer removeFromSuperlayer];
- _shapeLayer = nil;
- }
- }
- }
- #pragma mark - 显示/隐藏
- - (void)showMaskViewInView:(UIView *)view{
-
- self.alpha = 0;
- if (view != nil) {
- [view addSubview:self];
- }else{
- [[UIApplication sharedApplication].keyWindow addSubview:self];
- }
- [UIView animateWithDuration:0.3 animations:^{
- self.alpha = 1;
- }];
- }
- - (void)dismissMaskView{
- [UIView animateWithDuration:0.3 animations:^{
- self.alpha = 0;
- } completion:^(BOOL finished) {
- [self removeFromSuperview];
- }];
- }
- - (void)refreshMask {
-
- UIBezierPath *overlayPath = [self generateOverlayPath];
- self.overlayPath = overlayPath;
- }
- - (UIBezierPath *)generateOverlayPath {
-
- UIBezierPath *overlayPath = [UIBezierPath bezierPathWithRect:self.bounds];
- [overlayPath setUsesEvenOddFillRule:YES];
-
- return overlayPath;
- }
- #pragma mark - 懒加载Getter Methods
- - (UIBezierPath *)overlayPath {
- if (!_overlayPath) {
- _overlayPath = [self generateOverlayPath];
- }
-
- return _overlayPath;
- }
- - (CAShapeLayer *)fillLayer {
- if (!_fillLayer) {
- CAShapeLayer *fillLayer = [CAShapeLayer layer];
- fillLayer.frame = self.bounds;
- [self.layer addSublayer:fillLayer];
-
- _fillLayer = fillLayer;
- }
-
- return _fillLayer;
- }
- - (NSMutableArray *)transparentPaths {
- if (!_transparentPaths) {
- _transparentPaths = [NSMutableArray array];
- }
- return _transparentPaths;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|