KSBaseGuideView.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // KSBaseGuideView.m
  3. // KulexiuSchoolStudent
  4. //
  5. // Created by 王智 on 2024/7/26.
  6. //
  7. #import "KSBaseGuideView.h"
  8. @interface KSBaseGuideView ()
  9. @property (nonatomic, strong) UIImageView *displayImageView;
  10. // 跳过引导
  11. @property (nonatomic, strong) UIButton *skipButton;
  12. // 下一步
  13. @property (nonatomic, strong) UIButton *nextButton;
  14. // 再看一遍
  15. @property (nonatomic, strong) UIButton *replayButton;
  16. // 完成按钮
  17. @property (nonatomic, strong) UIButton *finishButton;
  18. @property (nonatomic, strong) KSBaseGuideModel *guideModel;
  19. @property (nonatomic, assign) BOOL isLastPage;
  20. @property (nonatomic, strong) NSString *skipButtonImage;
  21. @property (nonatomic, strong) NSString *nextButtonImage;
  22. @property (nonatomic, strong) NSString *replayButtonImage;
  23. @property (nonatomic, strong) NSString *finishButtonImage;
  24. @end
  25. @implementation KSBaseGuideView
  26. - (instancetype)initWithFrame:(CGRect)frame {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. self.backgroundColor = [UIColor clearColor];
  30. [self addTapGesture];
  31. }
  32. return self;
  33. }
  34. - (void)addTapGesture {
  35. UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
  36. [self addGestureRecognizer:gesture];
  37. }
  38. - (void)tapAction {
  39. if (self.guideModel.currentIndex == self.guideModel.totalIndex - 1) {
  40. if (self.callback) {
  41. self.callback(KS_NEXTSHOW_SKIP);
  42. }
  43. }
  44. else {
  45. if (self.callback) {
  46. self.callback(KS_NEXTSHOW_NEXT);
  47. }
  48. }
  49. }
  50. - (void)configskipButtonImage:(NSString *)skipButtonImage nextButtonImage:(NSString *)nextButtonImage replayButtonImage:(NSString *)replayButtonImage finishButtonImage:(NSString *)finishButtonImage {
  51. self.skipButtonImage = skipButtonImage;
  52. self.nextButtonImage = nextButtonImage;
  53. self.replayButtonImage = replayButtonImage;
  54. self.finishButtonImage = finishButtonImage;
  55. }
  56. - (void)addGuideLayerWithModel:(KSBaseGuideModel *)guideModel {
  57. if (guideModel.currentIndex == guideModel.totalIndex - 1) {
  58. self.isLastPage = YES;
  59. }
  60. else {
  61. self.isLastPage = NO;
  62. }
  63. [self clear];
  64. [self drawMaskWithModel:guideModel];
  65. [self addExtendBubble];
  66. [self addShowViews];
  67. }
  68. - (void)clear {
  69. [self removeAllSubViews];
  70. [self removeallSubLayers];
  71. }
  72. - (void)removeallSubLayers {
  73. [self.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
  74. }
  75. - (void)drawMaskWithModel:(KSBaseGuideModel *)guideModel {
  76. self.guideModel = guideModel;
  77. CGFloat addSize = 0;
  78. CGRect guideFrame = guideModel.guideViewFrame;
  79. CGRect guideViewFrame = CGRectMake(CGRectGetMinX(guideFrame) - addSize, CGRectGetMinY(guideFrame) - addSize , CGRectGetWidth(guideFrame) + addSize * 2, CGRectGetHeight(guideFrame) + addSize * 2);
  80. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.frame cornerRadius:0];
  81. // 镂空
  82. UIBezierPath *carvedPath = [UIBezierPath bezierPathWithRoundedRect:guideViewFrame cornerRadius:10.0f];
  83. [path appendPath:carvedPath];
  84. [path setUsesEvenOddFillRule:YES];
  85. CAShapeLayer *fillLayer = [CAShapeLayer layer];
  86. fillLayer.path = path.CGPath;
  87. // 中间镂空 填充规则
  88. fillLayer.fillRule = kCAFillRuleEvenOdd;
  89. fillLayer.fillColor = HexRGBAlpha(0x000000, 0.68f).CGColor;
  90. [self.layer addSublayer:fillLayer];
  91. }
  92. - (void)addExtendBubble {
  93. UIImage *displayImage = [UIImage imageNamed:self.guideModel.displayImageName];
  94. self.displayImageView = [[UIImageView alloc] initWithImage:displayImage];
  95. self.displayImageView.frame = self.guideModel.imageFrame;
  96. [self addSubview:self.displayImageView];
  97. }
  98. - (void)addShowViews {
  99. if (self.isLastPage == NO) {
  100. if (self.hideSkipButton == NO) {
  101. // 跳过
  102. [self addSubview:self.skipButton];
  103. CGFloat leftSpace = 20;
  104. [self.skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
  105. make.top.mas_equalTo(self.mas_top).offset(20);
  106. make.width.mas_equalTo(48);
  107. make.height.mas_equalTo(24);
  108. make.left.mas_equalTo(self.mas_left).offset(leftSpace);
  109. }];
  110. }
  111. [self addSubview:self.nextButton];
  112. [self.nextButton setTitle:[NSString stringWithFormat:@"下一步 (%zd/%zd)", self.guideModel.currentIndex+1, self.guideModel.totalIndex] forState:UIControlStateNormal];
  113. self.nextButton.frame = self.guideModel.buttonFrame;
  114. }
  115. else {
  116. // 显示跳过按钮
  117. [self addSubview:self.skipButton];
  118. CGFloat leftSpace = 20;
  119. [self.skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
  120. make.top.mas_equalTo(self.mas_top).offset(20);
  121. make.width.mas_equalTo(48);
  122. make.height.mas_equalTo(24);
  123. make.left.mas_equalTo(self.mas_left).offset(leftSpace);
  124. }];
  125. // 完成
  126. [self addSubview:self.finishButton];
  127. self.finishButton.frame = self.guideModel.buttonFrame;
  128. // 再来一次
  129. [self addSubview:self.replayButton];
  130. [self.replayButton mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.left.mas_equalTo(self.finishButton.mas_right).offset(14);
  132. make.width.mas_equalTo(82);
  133. make.height.mas_equalTo(32);
  134. make.centerY.mas_equalTo(self.finishButton.mas_centerY);
  135. }];
  136. }
  137. }
  138. - (void)skipGuideAction {
  139. if (self.callback) {
  140. self.callback(KS_NEXTSHOW_SKIP);
  141. }
  142. }
  143. - (void)replayGuideAction {
  144. if (self.callback) {
  145. self.callback(KS_NEXTSHOW_REPLAY);
  146. }
  147. }
  148. - (void)nextGuideAction {
  149. if (self.callback) {
  150. self.callback(KS_NEXTSHOW_NEXT);
  151. }
  152. }
  153. - (UIButton *)skipButton {
  154. if (!_skipButton) {
  155. _skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
  156. [_skipButton setBackgroundColor:[UIColor clearColor]];
  157. [_skipButton setBackgroundImage:[UIImage imageNamed:self.skipButtonImage] forState:UIControlStateNormal];
  158. [_skipButton addTarget:self action:@selector(skipGuideAction) forControlEvents:UIControlEventTouchUpInside];
  159. _skipButton.adjustsImageWhenHighlighted = NO;
  160. }
  161. return _skipButton;
  162. }
  163. - (UIButton *)replayButton {
  164. if (!_replayButton) {
  165. _replayButton = [UIButton buttonWithType:UIButtonTypeCustom];
  166. [_replayButton setTitle:@"再看一遍" forState:UIControlStateNormal];
  167. [_replayButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
  168. [_replayButton setTitleColor:HexRGB(0xFFFFFF) forState:UIControlStateNormal];
  169. [_replayButton.titleLabel setFont:[UIFont systemFontOfSize:13.0f weight:UIFontWeightSemibold]];
  170. [_replayButton setBackgroundColor:[UIColor clearColor]];
  171. [_replayButton setBackgroundImage:[UIImage imageNamed:self.replayButtonImage] forState:UIControlStateNormal];
  172. _replayButton.adjustsImageWhenHighlighted = NO;
  173. [_replayButton addTarget:self action:@selector(replayGuideAction) forControlEvents:UIControlEventTouchUpInside];
  174. }
  175. return _replayButton;
  176. }
  177. - (UIButton *)nextButton {
  178. if (!_nextButton) {
  179. _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
  180. [_nextButton setTitleColor:HexRGB(0x00807A) forState:UIControlStateNormal];
  181. [_nextButton.titleLabel setFont:[UIFont systemFontOfSize:13.0f weight:UIFontWeightSemibold]];
  182. [_nextButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
  183. [_nextButton setBackgroundImage:[UIImage imageNamed:self.nextButtonImage] forState:UIControlStateNormal];
  184. _nextButton.adjustsImageWhenHighlighted = NO;
  185. [_nextButton addTarget:self action:@selector(nextGuideAction) forControlEvents:UIControlEventTouchUpInside];
  186. }
  187. return _nextButton;
  188. }
  189. - (UIButton *)finishButton {
  190. if (!_finishButton) {
  191. _finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
  192. [_finishButton setTitle:@"完成" forState:UIControlStateNormal];
  193. [_finishButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
  194. [_finishButton setTitleColor:HexRGB(0x00807A) forState:UIControlStateNormal];
  195. [_finishButton.titleLabel setFont:[UIFont systemFontOfSize:13.0f weight:UIFontWeightSemibold]];
  196. [_finishButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
  197. [_finishButton setBackgroundImage:[UIImage imageNamed:self.finishButtonImage] forState:UIControlStateNormal];
  198. _finishButton.adjustsImageWhenHighlighted = NO;
  199. [_finishButton addTarget:self action:@selector(skipGuideAction) forControlEvents:UIControlEventTouchUpInside];
  200. }
  201. return _finishButton;
  202. }
  203. /*
  204. // Only override drawRect: if you perform custom drawing.
  205. // An empty implementation adversely affects performance during animation.
  206. - (void)drawRect:(CGRect)rect {
  207. // Drawing code
  208. }
  209. */
  210. @end