CNDownSlideView.swift 3.95 KB
//
//  CNDownSlideView.swift
//  metaCode
//
//  Created by daojian on 2023/5/30.
//

import Foundation
import UIKit
import WebKit

typealias backBlock = ()->Void;

///下滑显示的View
class CNDownSlideView:UIView{
    //返回首页
    var backAction:backBlock?
    
    //MARK: - 懒加载
    lazy var contentView:UIView = {
        
        let contentView = UIView.init(frame: CGRect(x: 0, y: 0, width: self.width, height: self.height-KTabBarHeight))
        contentView.backgroundColor = UIColor.clear
        
//        let path:UIBezierPath = UIBezierPath.init(roundedRect: contentView.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize.init(width: 20, height: 20))
//
//        let maskLayer:CAShapeLayer = CAShapeLayer()
//        maskLayer.frame = contentView.bounds
//        maskLayer.path  = path.cgPath
//        contentView.layer.mask = maskLayer
        
//        let imageView = UIImageView(frame: CGRect(x: 10, y: 20, width: contentView.width - 20, height: contentView.height - 40))
//        imageView.image = UIImage(named: "homePage_collectMoneyImage")
//        contentView.addSubview(imageView)
        
        return contentView
    }()
    
    //h5网页
    // TODO: H:-10原为self.height-KTabBarHeight,但有白条,改成了-10
    lazy var webView: WKWebView = {
        let webView = WKWebView.init(frame: CGRectMake(0, 0, self.width, self.height-KTabBarHeight-10))
        webView.scrollView.showsVerticalScrollIndicator = false
        webView.scrollView.showsHorizontalScrollIndicator = false
        webView.backgroundColor = .clear
        webView.isOpaque = false
        return webView
    }()
    
    var _url:String?
    var url:String? {
        get{
            return _url
        }
        set{
            _url = newValue!
            if (_url != ""){
                let webUrl: URL! = URL.init(string: _url!)
                webView.load(URLRequest.init(url:webUrl))
            }
        }
    }
    
    lazy var topView:UIView = {
        
        let tempTopView = UIView(frame: CGRect(x:0, y:20, width: self.width - 50, height: 70))
        
        return tempTopView
    }()
    
    lazy var bottomView:UIView = {
        
        let bottomView = UIView(frame: CGRect(x:0, y:self.height - KTabBarHeight, width: self.width, height: KTabBarHeight))
        bottomView.backgroundColor = .white
        bottomView.isHidden = true
        
        let path:UIBezierPath = UIBezierPath.init(roundedRect: bottomView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize.init(width: 20, height: 20))
        
        let maskLayer:CAShapeLayer = CAShapeLayer()
        maskLayer.frame = bottomView.bounds
        maskLayer.path  = path.cgPath
        bottomView.layer.mask = maskLayer
        
        let backBtn: UIButton = UIButton(type: .custom)
        backBtn.frame = CGRectMake(0, 0, 100, 30)
        backBtn.centerX = bottomView.width/2
        backBtn.centerY = bottomView.height/2
        backBtn.setImage(UIImage(named: "homePage_backhome_icon"), for: .normal)
        backBtn.setTitle("回到首页", for: .normal)
        backBtn.setTitleColor(UIColor("#2076FF"), for: .normal)
        backBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        backBtn.addTarget(self, action: #selector(backBtnAction), for: .touchUpInside)
        backBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 8)
        bottomView.addSubview(backBtn)
        
        return bottomView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    ///初始化UI
    func initUI(){
        self.addSubview(self.contentView)
        self.contentView.addSubview(self.webView)
//        self.addSubview(self.topView)
        self.addSubview(self.bottomView)
        
    }
    
    @objc func backBtnAction(){
        self.backAction!()
    }
    
}