CNDownSlideView.swift 6 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))
        contentView.backgroundColor = UIColor.clear
        contentView.isUserInteractionEnabled = true
        return contentView
    }()
    
    //h5网页
    lazy var webView: CNWebView = {
        let configuration = WKWebViewConfiguration()
        configuration.allowsInlineMediaPlayback = true
        let preferences = WKPreferences()
        preferences.javaScriptCanOpenWindowsAutomatically = true
        preferences.javaScriptEnabled = true
        configuration.preferences.minimumFontSize = 0.0
        configuration.preferences = preferences
        configuration.userContentController = WKUserContentController()

        var url:URL?
        if (_downPath != ""){
            url = URL.init(string:_downPath!)//"https://managemall.cnlive.com/html/mall/1/#/pages/main/goodsList/goodsList?scene=app"cn)//_downPath!)//
        }else{
            url = nil
        }
        let webView = CNWebView(frame: CGRectMake(0, 0, Width, self.height-DownSlideBottomViewHeight), configuration: configuration,url: url)
        
        webView.cnWebView.scrollView.showsVerticalScrollIndicator = false
        webView.cnWebView.scrollView.showsHorizontalScrollIndicator = false
        return webView
    }()
    
    lazy var upSlideVC:CNUpSlideViewController  = {
        let upSlideVC = CNUpSlideViewController.init()
        upSlideVC.view.width = KSreenWidth
        upSlideVC.view.height = KSreenHeight-DownSlideBottomViewHeight
        return upSlideVC
    }()
    
    lazy var shopConsoleVC:CNLiveShopConsoleController  = {
        let shopConsoleVC = CNLiveShopConsoleController.init()
        shopConsoleVC.view.width = KSreenWidth
        shopConsoleVC.view.height = KSreenHeight-DownSlideBottomViewHeight
        return shopConsoleVC
    }()
    
    //IDmodel
    var _idModel : StandListModel?
    var idModel : StandListModel? {
        get{
            _idModel
        }
        set{
            _idModel = newValue
        }
        
    }
    
    var type:Int?
    
    var _downPath:String?
    var downPath:String? {
        get{
            return _downPath
        }
        set{
            _downPath = newValue!
            self.subviews.forEach { view in
                if view != bottomView{
                    view.removeFromSuperview()
                }else{
//                    print("\(view)")
                }
            }//会删除button
            
            if(type == UpDownTypeH5){
                if _downPath?.count ?? 0 > 0{
                    self.addSubview(self.webView)
                }
            }else if(type == UpDownTypePage){
                self.addSubview(self.upSlideVC.view)
                self.upSlideVC.idModel = _idModel
                self.upSlideVC.downPath = _downPath
                
            }else if(type == UpDownTypeShopDetail){
                
            }else if(type == UpDownTypeShopControlConsole){
                self.addSubview(self.shopConsoleVC.view)

            }else{
                
            }
        }
    }
    
    lazy var bottomView:UIView = {
        
        let bottomView = UIView(frame: CGRect(x:0, y:self.height - DownSlideBottomViewHeight, width: self.width, height: DownSlideBottomViewHeight))
        bottomView.backgroundColor = HexColor("#FFFFFF")
        bottomView.isHidden = true
        // 设置阴影效果
        bottomView.layer.masksToBounds = false
        //定义view的阴影颜色
        bottomView.layer.shadowColor = UIColor(red: 0.55, green: 0.68, blue: 0.86, alpha: 0.1).cgColor
        //定义view的阴影透明度,注意:如果view没有设置背景色阴影也是不会显示的
        bottomView.layer.shadowOpacity = 1
        //定义view的阴影宽度,模糊计算的半径
        bottomView.layer.shadowRadius = 15.0
        //阴影偏移量
        bottomView.layer.shadowOffset = CGSize(width: 0, height: -3)
        bottomView.layer.cornerRadius = 50
        bottomView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        
        let backBtn: UIButton = UIButton(type: .custom)
        backBtn.frame = CGRectMake(0, 0, 100, DownSlideBottomViewHeight)
        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)
        bottomView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(backBtnAction))
        bottomView.addGestureRecognizer(tap)
        return bottomView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(slideViewTap))
        tap.cancelsTouchesInView = false
        self.addGestureRecognizer(tap)
        self.initUI()
    }
    @objc func slideViewTap() {
        print("slideViewTap")
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    ///初始化UI
    func initUI(){
        self.isUserInteractionEnabled = true
        self.addSubview(self.contentView)

        self.addSubview(self.bottomView)
    }
    
    @objc func backBtnAction(){
        self.backAction!()
    }
    
}