溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS開發(fā)添加新手引導的代碼詳解

發(fā)布時間:2020-07-20 16:02:41 來源:億速云 閱讀:318 作者:小豬 欄目:移動開發(fā)

這篇文章主要講解了iOS開發(fā)添加新手引導的代碼詳解,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

往往項目中經常出現此類需求

iOS開發(fā)添加新手引導的代碼詳解

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

//
// 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(@"引導狀態(tài)可點擊");

}
@end

相應頁面直接添加

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

dispatch_async(dispatch_get_main_queue(), ^{

[guide showGuide: self .view]; 

});

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

看完上述內容,是不是對iOS開發(fā)添加新手引導的代碼詳解有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

ios
AI