KSGuideMaskView.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // KSGuideMaskView.m
  3. // MusicGradeExam
  4. //
  5. // Created by Kyle on 2020/7/28.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "KSGuideMaskView.h"
  9. NSInteger countNum = 0;
  10. @interface KSGuideMaskView ()
  11. /// 图层
  12. @property (nonatomic, weak) CAShapeLayer *fillLayer;
  13. /// 路径
  14. @property (nonatomic, strong) UIBezierPath *overlayPath;
  15. /// 透明区数组
  16. @property (nonatomic, strong) NSMutableArray *transparentPaths;
  17. /// 点击计数
  18. @property (nonatomic, assign) NSInteger index;
  19. /// tips数组
  20. @property (nonatomic, strong) NSMutableArray *tipsArray;
  21. @property (nonatomic, assign) NSInteger shaperLayerIndex;
  22. @property (nonatomic, strong) CAShapeLayer *shapeLayer;
  23. @end
  24. @implementation KSGuideMaskView
  25. - (instancetype)initWithFrame:(CGRect)frame {
  26. self = [super initWithFrame: [UIScreen mainScreen].bounds];
  27. if (self) {
  28. [self setUp];
  29. }
  30. return self;
  31. }
  32. - (void)setUp {
  33. self.index = 0;
  34. self.backgroundColor = [UIColor clearColor];
  35. UIColor *maskColor = [UIColor colorWithWhite:0 alpha:0.61];
  36. self.fillLayer.path = self.overlayPath.CGPath;
  37. self.fillLayer.fillRule = kCAFillRuleEvenOdd;
  38. self.fillLayer.fillColor = maskColor.CGColor;
  39. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClickedMaskView)];
  40. [self addGestureRecognizer:tapGesture];
  41. }
  42. - (void)layoutSubviews {
  43. [super layoutSubviews];
  44. }
  45. - (void)addTips:(NSArray *)tipsArray transparentRect:(NSArray *)rectArray shaperLayerIndex:(NSInteger)index {
  46. if (tipsArray.count != rectArray.count) {
  47. return;
  48. }
  49. self.shaperLayerIndex = index;
  50. self.tipsArray = [NSMutableArray arrayWithArray:tipsArray];
  51. self.transparentPaths = [NSMutableArray arrayWithArray:rectArray];
  52. UIBezierPath *path = _transparentPaths[0];
  53. [self addTips:_tipsArray[0] withFrame:path.bounds];
  54. [self addTransparentPath:_transparentPaths[0]];
  55. }
  56. - (void)tapClickedMaskView {
  57. _index++;
  58. if (_index < _tipsArray.count) {
  59. [self refreshMask];
  60. [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  61. UIBezierPath *path = _transparentPaths[_index];
  62. [self addTransparentPath:_transparentPaths[_index]];
  63. [self addTips:_tipsArray[_index] withFrame:path.bounds];
  64. } else {
  65. countNum = 0;
  66. [self dismissMaskView];
  67. }
  68. }
  69. - (void)addTips:(NSString *)tipsMessage withFrame:(CGRect)frame {
  70. UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
  71. label.text = tipsMessage;
  72. label.textAlignment = NSTextAlignmentCenter;
  73. label.font = [UIFont systemFontOfSize:15.0f];
  74. label.textColor = HexRGB(0xffffff);
  75. [self addSubview:label];
  76. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  77. make.left.right.mas_equalTo(self);
  78. make.top.mas_equalTo(CGRectGetMaxY(frame) + 20);
  79. }];
  80. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  81. button.layer.borderWidth = 1.0f;
  82. button.layer.borderColor = HexRGB(0xffffff).CGColor;
  83. button.layer.cornerRadius = 20.0f;
  84. [button setTitle:@"知道啦" forState:UIControlStateNormal];
  85. button.userInteractionEnabled = NO;
  86. // [button addTarget:self action:@selector(tapClickedMaskView) forControlEvents:UIControlEventTouchUpInside];
  87. [self addSubview:button];
  88. [button mas_makeConstraints:^(MASConstraintMaker *make) {
  89. make.width.mas_equalTo(110);
  90. make.height.mas_equalTo(40);
  91. make.top.mas_equalTo(label.mas_bottom).offset(28);
  92. make.centerX.mas_equalTo(self);
  93. }];
  94. }
  95. - (void)addTransparentPath:(UIBezierPath *)transparentPath {
  96. [self.overlayPath appendPath:transparentPath];
  97. self.fillLayer.path = self.overlayPath.CGPath;
  98. if (_index == self.shaperLayerIndex) {
  99. CGRect frame = transparentPath.bounds;
  100. frame.size.width += 20;
  101. frame.size.height += 20;
  102. frame.origin.x -= 10;
  103. frame.origin.y -= 10;
  104. UIBezierPath *newPath = [UIBezierPath bezierPathWithOvalInRect:frame];
  105. _shapeLayer = [CAShapeLayer layer];
  106. _shapeLayer.frame = self.bounds;
  107. _shapeLayer.path = newPath.CGPath;
  108. _shapeLayer.lineWidth = 2.0f;
  109. _shapeLayer.strokeColor = HexRGB(0xffffff).CGColor;
  110. _shapeLayer.fillColor = [UIColor clearColor].CGColor;
  111. _shapeLayer.lineDashPattern = @[@(10), @(10)];
  112. [self.layer addSublayer:_shapeLayer];
  113. }
  114. else {
  115. if (_shapeLayer) {
  116. [_shapeLayer removeFromSuperlayer];
  117. _shapeLayer = nil;
  118. }
  119. }
  120. }
  121. #pragma mark - 显示/隐藏
  122. - (void)showMaskViewInView:(UIView *)view{
  123. self.alpha = 0;
  124. if (view != nil) {
  125. [view addSubview:self];
  126. }else{
  127. [[UIApplication sharedApplication].keyWindow addSubview:self];
  128. }
  129. [UIView animateWithDuration:0.3 animations:^{
  130. self.alpha = 1;
  131. }];
  132. }
  133. - (void)dismissMaskView{
  134. [UIView animateWithDuration:0.3 animations:^{
  135. self.alpha = 0;
  136. } completion:^(BOOL finished) {
  137. [self removeFromSuperview];
  138. }];
  139. }
  140. - (void)refreshMask {
  141. UIBezierPath *overlayPath = [self generateOverlayPath];
  142. self.overlayPath = overlayPath;
  143. }
  144. - (UIBezierPath *)generateOverlayPath {
  145. UIBezierPath *overlayPath = [UIBezierPath bezierPathWithRect:self.bounds];
  146. [overlayPath setUsesEvenOddFillRule:YES];
  147. return overlayPath;
  148. }
  149. #pragma mark - 懒加载Getter Methods
  150. - (UIBezierPath *)overlayPath {
  151. if (!_overlayPath) {
  152. _overlayPath = [self generateOverlayPath];
  153. }
  154. return _overlayPath;
  155. }
  156. - (CAShapeLayer *)fillLayer {
  157. if (!_fillLayer) {
  158. CAShapeLayer *fillLayer = [CAShapeLayer layer];
  159. fillLayer.frame = self.bounds;
  160. [self.layer addSublayer:fillLayer];
  161. _fillLayer = fillLayer;
  162. }
  163. return _fillLayer;
  164. }
  165. - (NSMutableArray *)transparentPaths {
  166. if (!_transparentPaths) {
  167. _transparentPaths = [NSMutableArray array];
  168. }
  169. return _transparentPaths;
  170. }
  171. /*
  172. // Only override drawRect: if you perform custom drawing.
  173. // An empty implementation adversely affects performance during animation.
  174. - (void)drawRect:(CGRect)rect {
  175. // Drawing code
  176. }
  177. */
  178. @end