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];

    });

可根據不同需求進行不同的布局,核心代碼就是添加路徑

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM