iOS開發添加新手引導

每每項目中常常出現此類需求app

用戶經過點擊引導按鈕可響應頁面附帶按鈕的點擊事件。async

 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

 

相應頁面直接添加ide

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

    dispatch_async(dispatch_get_main_queue(), ^{ui

        [guide showGuide:self.view];spa

    });code

可根據不一樣需求進行不一樣的佈局,核心代碼就是添加路徑blog

 

相關文章
相關標籤/搜索