| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- //
- // KSBaseGuideView.m
- // KulexiuSchoolStudent
- //
- // Created by 王智 on 2024/7/26.
- //
- #import "KSBaseGuideView.h"
- @interface KSBaseGuideView ()
- @property (nonatomic, strong) UIImageView *displayImageView;
- // 跳过引导
- @property (nonatomic, strong) UIButton *skipButton;
- // 下一步
- @property (nonatomic, strong) UIButton *nextButton;
- // 再看一遍
- @property (nonatomic, strong) UIButton *replayButton;
- // 完成按钮
- @property (nonatomic, strong) UIButton *finishButton;
- @property (nonatomic, strong) KSBaseGuideModel *guideModel;
- @property (nonatomic, assign) BOOL isLastPage;
- @property (nonatomic, strong) NSString *skipButtonImage;
- @property (nonatomic, strong) NSString *nextButtonImage;
- @property (nonatomic, strong) NSString *replayButtonImage;
- @property (nonatomic, strong) NSString *finishButtonImage;
- @end
- @implementation KSBaseGuideView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor clearColor];
- [self addTapGesture];
- }
- return self;
- }
- - (void)addTapGesture {
- UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
- [self addGestureRecognizer:gesture];
- }
- - (void)tapAction {
- if (self.guideModel.currentIndex == self.guideModel.totalIndex - 1) {
- if (self.callback) {
- self.callback(KS_NEXTSHOW_SKIP);
- }
- }
- else {
- if (self.callback) {
- self.callback(KS_NEXTSHOW_NEXT);
- }
- }
- }
- - (void)configskipButtonImage:(NSString *)skipButtonImage nextButtonImage:(NSString *)nextButtonImage replayButtonImage:(NSString *)replayButtonImage finishButtonImage:(NSString *)finishButtonImage {
- self.skipButtonImage = skipButtonImage;
- self.nextButtonImage = nextButtonImage;
- self.replayButtonImage = replayButtonImage;
- self.finishButtonImage = finishButtonImage;
- }
- - (void)addGuideLayerWithModel:(KSBaseGuideModel *)guideModel {
- if (guideModel.currentIndex == guideModel.totalIndex - 1) {
- self.isLastPage = YES;
- }
- else {
- self.isLastPage = NO;
- }
- [self clear];
- [self drawMaskWithModel:guideModel];
- [self addExtendBubble];
- [self addShowViews];
- }
- - (void)clear {
- [self removeAllSubViews];
- [self removeallSubLayers];
- }
- - (void)removeallSubLayers {
- [self.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
- }
- - (void)drawMaskWithModel:(KSBaseGuideModel *)guideModel {
- self.guideModel = guideModel;
- CGFloat addSize = 0;
- CGRect guideFrame = guideModel.guideViewFrame;
- CGRect guideViewFrame = CGRectMake(CGRectGetMinX(guideFrame) - addSize, CGRectGetMinY(guideFrame) - addSize , CGRectGetWidth(guideFrame) + addSize * 2, CGRectGetHeight(guideFrame) + addSize * 2);
- UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.frame cornerRadius:0];
- // 镂空
- UIBezierPath *carvedPath = [UIBezierPath bezierPathWithRoundedRect:guideViewFrame cornerRadius:10.0f];
- [path appendPath:carvedPath];
- [path setUsesEvenOddFillRule:YES];
-
- CAShapeLayer *fillLayer = [CAShapeLayer layer];
- fillLayer.path = path.CGPath;
- // 中间镂空 填充规则
- fillLayer.fillRule = kCAFillRuleEvenOdd;
- fillLayer.fillColor = HexRGBAlpha(0x000000, 0.68f).CGColor;
- [self.layer addSublayer:fillLayer];
- }
- - (void)addExtendBubble {
- UIImage *displayImage = [UIImage imageNamed:self.guideModel.displayImageName];
- self.displayImageView = [[UIImageView alloc] initWithImage:displayImage];
- self.displayImageView.frame = self.guideModel.imageFrame;
- [self addSubview:self.displayImageView];
- }
- - (void)addShowViews {
- if (self.isLastPage == NO) {
- if (self.hideSkipButton == NO) {
- // 跳过
- [self addSubview:self.skipButton];
- CGFloat leftSpace = 20;
- [self.skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(self.mas_top).offset(20);
- make.width.mas_equalTo(48);
- make.height.mas_equalTo(24);
- make.left.mas_equalTo(self.mas_left).offset(leftSpace);
- }];
- }
-
- [self addSubview:self.nextButton];
- [self.nextButton setTitle:[NSString stringWithFormat:@"下一步 (%zd/%zd)", self.guideModel.currentIndex+1, self.guideModel.totalIndex] forState:UIControlStateNormal];
- self.nextButton.frame = self.guideModel.buttonFrame;
- }
- else {
- // 显示跳过按钮
- [self addSubview:self.skipButton];
- CGFloat leftSpace = 20;
- [self.skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(self.mas_top).offset(20);
- make.width.mas_equalTo(48);
- make.height.mas_equalTo(24);
- make.left.mas_equalTo(self.mas_left).offset(leftSpace);
- }];
-
- // 完成
- [self addSubview:self.finishButton];
- self.finishButton.frame = self.guideModel.buttonFrame;
-
- // 再来一次
- [self addSubview:self.replayButton];
- [self.replayButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.finishButton.mas_right).offset(14);
- make.width.mas_equalTo(82);
- make.height.mas_equalTo(32);
- make.centerY.mas_equalTo(self.finishButton.mas_centerY);
- }];
-
- }
- }
- - (void)skipGuideAction {
- if (self.callback) {
- self.callback(KS_NEXTSHOW_SKIP);
- }
- }
- - (void)replayGuideAction {
- if (self.callback) {
- self.callback(KS_NEXTSHOW_REPLAY);
- }
- }
- - (void)nextGuideAction {
- if (self.callback) {
- self.callback(KS_NEXTSHOW_NEXT);
- }
- }
- - (UIButton *)skipButton {
- if (!_skipButton) {
- _skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_skipButton setBackgroundColor:[UIColor clearColor]];
- [_skipButton setBackgroundImage:[UIImage imageNamed:self.skipButtonImage] forState:UIControlStateNormal];
- [_skipButton addTarget:self action:@selector(skipGuideAction) forControlEvents:UIControlEventTouchUpInside];
- _skipButton.adjustsImageWhenHighlighted = NO;
- }
- return _skipButton;
- }
- - (UIButton *)replayButton {
- if (!_replayButton) {
- _replayButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_replayButton setTitle:@"再看一遍" forState:UIControlStateNormal];
- [_replayButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
- [_replayButton setTitleColor:HexRGB(0xFFFFFF) forState:UIControlStateNormal];
- [_replayButton.titleLabel setFont:[UIFont systemFontOfSize:13.0f weight:UIFontWeightSemibold]];
- [_replayButton setBackgroundColor:[UIColor clearColor]];
- [_replayButton setBackgroundImage:[UIImage imageNamed:self.replayButtonImage] forState:UIControlStateNormal];
- _replayButton.adjustsImageWhenHighlighted = NO;
- [_replayButton addTarget:self action:@selector(replayGuideAction) forControlEvents:UIControlEventTouchUpInside];
- }
- return _replayButton;
- }
- - (UIButton *)nextButton {
- if (!_nextButton) {
- _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_nextButton setTitleColor:HexRGB(0x00807A) forState:UIControlStateNormal];
- [_nextButton.titleLabel setFont:[UIFont systemFontOfSize:13.0f weight:UIFontWeightSemibold]];
- [_nextButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
- [_nextButton setBackgroundImage:[UIImage imageNamed:self.nextButtonImage] forState:UIControlStateNormal];
- _nextButton.adjustsImageWhenHighlighted = NO;
- [_nextButton addTarget:self action:@selector(nextGuideAction) forControlEvents:UIControlEventTouchUpInside];
- }
- return _nextButton;
- }
- - (UIButton *)finishButton {
- if (!_finishButton) {
- _finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_finishButton setTitle:@"完成" forState:UIControlStateNormal];
- [_finishButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
- [_finishButton setTitleColor:HexRGB(0x00807A) forState:UIControlStateNormal];
- [_finishButton.titleLabel setFont:[UIFont systemFontOfSize:13.0f weight:UIFontWeightSemibold]];
- [_finishButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
- [_finishButton setBackgroundImage:[UIImage imageNamed:self.finishButtonImage] forState:UIControlStateNormal];
- _finishButton.adjustsImageWhenHighlighted = NO;
- [_finishButton addTarget:self action:@selector(skipGuideAction) forControlEvents:UIControlEventTouchUpInside];
- }
- return _finishButton;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|