iOS开发添加新手引导

往往项目中经常出现此类需求

用户通过点击引导按钮可响应页面附带按钮的点击事件。

 1 //
 2 //  gzhGuideView.h
 3 //  GuideView
 4 //
 5 //  Created by 郭志贺 on 2020/5/29.
 6 //  Copyright © 2020 郭志贺. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 NS_ASSUME_NONNULL_BEGIN
12 
13 @interface gzhGuideView : UIView
14 
15 
16 -(void)showGuide:(UIView*)view;//显示引导
17 -(void)dismissGuide;//移除
18 
19 @end
20 
21 NS_ASSUME_NONNULL_END
 1 //
 2 //  gzhGuideView.m
 3 //  GuideView
 4 //
 5 //  Created by 郭志贺 on 2020/5/29.
 6 //  Copyright © 2020 郭志贺. All rights reserved.
 7 //
 8 
 9 #import "gzhGuideView.h"
10 
11 @implementation gzhGuideView
12 -(instancetype)initWithFrame:(CGRect)frame{
13 
14     if (self = [super initWithFrame:frame]) {
15         
16         self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
17         //主要代码 添加路径
18         UIBezierPath *path = [UIBezierPath bezierPathWithRect:frame];
19         // 这里添加第二个路径 需要扣除的部分
20         [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 150, 40) cornerRadius:5] bezierPathByReversingPath]];
21 
22         //渲染
23         CAShapeLayer *shapeLayer = [CAShapeLayer layer];
24         shapeLayer.path = path.CGPath;
25         [self.layer setMask:shapeLayer];
26         
27         //根据需求添加按钮 实现点击事件
28         UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
29         button.frame = CGRectMake(100, 100, 150, 40);
30         [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
31         button.layer.cornerRadius = 5.0f;
32         button.layer.masksToBounds = YES;
33         [self addSubview:button];
34     }
35     
36     return self;
37 }
38 
39 -(void)showGuide:(UIView *)view{//添加
40     
41     
42     [view.window addSubview:self];
43     [view.window bringSubviewToFront:self];
44     self.alpha = 1;
45 
46     
47 }
48 -(void)dismissGuide{//移除
49     
50     [self removeFromSuperview];
51     
52 }
53 -(void)buttonClick{
54     [self dismissGuide];
55     NSLog(@"引导状态可点击");
56     
57 }
58 @end

 

相应页面直接添加

 gzhGuideView * guide = [[gzhGuideView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];

    dispatch_async(dispatch_get_main_queue(), ^{

        [guide showGuide:self.view];

    });

可根据不同需求进行不同的布局,核心代码就是添加路径

 

 

posted @ 2020-05-29 14:16  郭志贺  阅读(755)  评论(0编辑  收藏  举报