温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS开发添加新手引导的代码详解

发布时间:2020-07-20 16:02:41 来源:亿速云 阅读:314 作者:小猪 栏目:移动开发

这篇文章主要讲解了iOS开发添加新手引导的代码详解,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

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

iOS开发添加新手引导的代码详解

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

//
// gzhGuideView.h
// GuideView
//
// Created by 郭志贺 on 2020/5/29.
// Copyright © 2020 郭志贺. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface gzhGuideView : UIView


-(void)showGuide:(UIView*)view;//显示引导
-(void)dismissGuide;//移除

@end

NS_ASSUME_NONNULL_END
#import "gzhGuideView.h"

@implementation gzhGuideView
-(instancetype)initWithFrame:(CGRect)frame{

  if (self = [super initWithFrame:frame]) {

    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    //主要代码 添加路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:frame];
    // 这里添加第二个路径 需要扣除的部分
    [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 150, 40) cornerRadius:5] bezierPathByReversingPath]];

    //渲染
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    [self.layer setMask:shapeLayer];

    //根据需求添加按钮 实现点击事件
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 150, 40);
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    button.layer.cornerRadius = 5.0f;
    button.layer.masksToBounds = YES;
    [self addSubview:button];
  }

  return self;
}

-(void)showGuide:(UIView *)view{//添加


  [view.window addSubview:self];
  [view.window bringSubviewToFront:self];
  self.alpha = 1;


}
-(void)dismissGuide{//移除

  [self removeFromSuperview];

}
-(void)buttonClick{
  [self dismissGuide];
  NSLog(@"引导状态可点击");

}
@end

相应页面直接添加

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

dispatch_async(dispatch_get_main_queue(), ^{

[guide showGuide: self .view]; 

});

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

看完上述内容,是不是对iOS开发添加新手引导的代码详解有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios
AI