CNHomePageGuideView.swift 2.83 KB
//
//  CNHomePageGuideView.swift
//  metaCode
//
//  Created by daojian on 2023/6/7.
//  首页引导

import Foundation

typealias skipGuideBlock = ()->Void

class CNHomePageGuideView: UIView{
    ///跳过引导事件回调
    var skipAction: skipGuideBlock?
    
   lazy var skipButtom: UIButton = {
       
       let skipButtom = UIButton(type: .custom)
       skipButtom.frame = CGRect(x: 0, y: 0, width: 90, height: 30)
       skipButtom.right = self.width - 30
       skipButtom.top   = KStatusBarHeight + 30
       skipButtom.setTitle("跳过引导", for: .normal)
       skipButtom.setTitleColor(.white, for: .normal)
       skipButtom.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: .medium)
       skipButtom.clipsToBounds = true
       skipButtom.layer.cornerRadius = skipButtom.height/2
       skipButtom.layer.borderColor = UIColor("#979797")?.cgColor
       skipButtom.layer.borderWidth = 0.5
       
      return skipButtom
        
    }()
    
    lazy var bottomLabel: UILabel = {
        let bottomLabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: 150, height: 20))
        bottomLabel.textColor = .white
        bottomLabel.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        bottomLabel.textAlignment = .center
        return bottomLabel
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initUI(){
        
        self.addSubview(self.skipButtom)
        
        self.backgroundColor = .black.withAlphaComponent(0.7)
        self.isUserInteractionEnabled = false
        
        //通过设置mask透明区域来实现镂空效果
        let path:UIBezierPath = UIBezierPath.init(rect: UIScreen.main.bounds)
        
        //rect 首页被镂空的区域
        let W = MainViewW
        let H = W*1.5
        let rect = CGRectMake(LeftGap, KSreenHeight*0.16,W, H)
        
        let appendPath: UIBezierPath = UIBezierPath.init(roundedRect: rect, cornerRadius: 20)
        path.append(appendPath.reversing())
        
        let shapeLayer: CAShapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath;
        self.layer.mask = shapeLayer
        
        //更新bottomLabel位置
        self.bottomLabel.centerX = self.centerX
        self.bottomLabel.top = CGRectGetMaxY(rect) + 20
        self.addSubview(self.bottomLabel)
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if((self.skipButtom.layer.hitTest(point)) != nil){
//            print("点击了跳过")
            self.skipAction!()
        }
        //让事件响应链进行执行
        return super.hitTest(point, with: event)
    }
    
    func setBottomText(text: String)->Void{
        self.bottomLabel.text = text
    }
}