zoukankan      html  css  js  c++  java
  • 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];

        });

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

  • 相关阅读:
    【洛谷6620】[省选联考 2020 A 卷] 组合数问题(下降幂)
    【AtCoder】AtCoder Grand Contest 033 解题报告
    【AtCoder】AtCoder Grand Contest 034 解题报告
    【洛谷5445】[APIO2019] 路灯(树套树)
    【LOJ6059】「2017 山东一轮集训 Day1」Sum(倍增优化数位DP+NTT)
    【LOJ6159】「美团 CodeM 初赛 Round A」最长树链(树的直径)
    重新入门的Polya定理
    【洛谷6105】[Ynoi2010] y-fast trie(set)
    【BZOJ4480】 [JSOI2013] 快乐的jyy(回文自动机裸题)
    【LOJ6172】Samjia 和大树(树形DP+猜结论)
  • 原文地址:https://www.cnblogs.com/guozhihe/p/12987221.html
Copyright © 2011-2022 走看看