1. 程式人生 > 程式設計 >iOS開發新增新手引導效果

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
//
// gzhGuideView.m
// GuideView
//
// Created by 郭志賀 on 2020/5/29.
// Copyright © 2020 郭志賀. All rights reserved.
//

#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,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,kScreenWidth,kScreenHeight)];

dispatch_async(dispatch_get_main_queue(),^{

[guide showGuide: self .view]; 

});

可根據不同需求進行不同的佈局,核心程式碼就是新增路徑

總結

到此這篇關於iOS開發新增新手引導的例項程式碼的文章就介紹到這了,更多相關ios新手引導內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!