CNLiveVodLandscapeGaoqingView.swift 9.28 KB
//
//  CNLiveVodLandscapeGaoqingView.swift
//  metaCode
//
//  Created by zx on 2025/3/28.
//  横屏高清

import Foundation

//标清 2 ,高清 3 ,超清 4
typealias CNLiveVodLandscapeGaoqingUpdateBlock = (_ gaoqing:Int)->Void

class CNLiveVodLandscapeGaoqingView:UIView,UIGestureRecognizerDelegate{
    
    var gaoqingUpdateBlock:CNLiveVodLandscapeGaoqingUpdateBlock?
    var currSeleIdx = 1//720
    //MARK: - 懒加载
    lazy var titleLabel: UILabel = {
        let titleLbl = UILabel()
        titleLbl.font = Font_14
        titleLbl.textColor = HexColor("#FFFFFF")
        titleLbl.text = "画质"
        return titleLbl
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = HexColor("#000000", alpha: 0.7)
        self.isUserInteractionEnabled = true
        self.initUI()
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapAction)))
        
        self.initGesture()
    }
    
    func initGesture(){
        self.isMultipleTouchEnabled = true

        // 双击手势
        var doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(doubleTapAction))
        doubleTapGesture.numberOfTapsRequired = 2 // 双击触发
        doubleTapGesture.delaysTouchesBegan = true
        doubleTapGesture.delaysTouchesEnded = true
        doubleTapGesture.numberOfTouchesRequired = 1

        var panGesture = UIPanGestureRecognizer(target: self, action: #selector(panAction))
        panGesture.delaysTouchesBegan = true
        panGesture.delaysTouchesEnded = true
        panGesture.maximumNumberOfTouches = 1
        panGesture.cancelsTouchesInView = true

        var tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        tapGesture.delaysTouchesBegan = true
        tapGesture.delaysTouchesEnded = true
        tapGesture.numberOfTouchesRequired = 1
        tapGesture.numberOfTapsRequired = 1
        tapGesture.require(toFail: doubleTapGesture)
        tapGesture.require(toFail: panGesture)
        self.addGestureRecognizer(tapGesture)
        self.addGestureRecognizer(doubleTapGesture)
        self.addGestureRecognizer(panGesture)

        var pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction))
        pinchGesture.delaysTouchesBegan = true
        self.addGestureRecognizer(pinchGesture)

        // 长按手势
        var longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction))
        longGesture.minimumPressDuration = 0.5// 长按触发时间(秒)
        self.addGestureRecognizer(longGesture)
    }
//    @objc func tapAction(){
////        self.isHidden = true
//        print("tapAction")
//    }
    @objc func pinchAction(){
//        self.isHidden = true
        print("pinchAction")
    }
    @objc func longPressAction(){
//        self.isHidden = true
        print("longPressAction")
    }
    @objc func doubleTapAction(){
//        self.isHidden = true
        print("doubleTapAction")
    }
    
    @objc func panAction(){
//        self.isHidden = true
        print("panAction")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        
        self.addSubview(self.titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(31)
            make.centerX.equalToSuperview()
        }
        
        var WH:CGFloat = 90.0
        let padding:CGFloat = 20.0
        let count:Int = 3
        let leftPadding:CGFloat = (KSreenHeight-WH*CGFloat(count)-padding*CGFloat(count-1))*0.5
        var title = "高清"
        var subTitle = "720P"

        for i in 0..<count{
            let expandBtn = UIButton.init(type: .custom)
            expandBtn.frame = CGRect(x: leftPadding+CGFloat(i)*(WH+padding), y: 0, width: WH, height: WH)
            expandBtn.centerY = KSreenWidth*0.5
            if i == 0{
                //标清480
                title = "标清"
                subTitle = "480P"
            }else if i == 1{
                //高清720
                title = "高清"
                subTitle = "720P"
            }else if i == 2{
                //超清1080
                title = "超清"
                subTitle = "1080P"
            }
            expandBtn.setTitle("", for: .normal)
            expandBtn.setTitle("", for: .selected)
            expandBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
            expandBtn.setTitleColor(HexColor("#478BFF"), for: .selected)
            expandBtn.titleLabel?.font = Font_18
            expandBtn.layer.cornerRadius = 12
            expandBtn.clipsToBounds = true
            expandBtn.tag = 100+i
            expandBtn.addTarget(self, action: #selector(gaoqingBtnAction), for: .touchUpInside)
            self.addSubview(expandBtn)
            
            let titleLbl = UILabel()
            titleLbl.font = Font_14
            titleLbl.text = title
            expandBtn.addSubview(titleLbl)
            titleLbl.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(30)
            }
            
            let subTitleLbl = UILabel()
            subTitleLbl.font = Font_10
            subTitleLbl.text = subTitle
            expandBtn.addSubview(subTitleLbl)
            subTitleLbl.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(titleLbl.snp.bottom).offset(3)
            }
            
            if i == self.currSeleIdx{
                expandBtn.backgroundColor = HexColor("#D3E4FF")
                expandBtn.layer.borderWidth = 0.65
                expandBtn.layer.borderColor = HexColor("#478BFF")?.cgColor
                expandBtn.isSelected = true
                titleLbl.textColor = HexColor("#0668E7")
                subTitleLbl.textColor = HexColor("#0668E7")
            }else{
                expandBtn.backgroundColor = HexColor("#000000")
                expandBtn.layer.borderWidth = 0
                expandBtn.layer.borderColor = HexColor("#000000")?.cgColor
                expandBtn.isSelected = false
                titleLbl.textColor = HexColor("#FFFFFF")
                subTitleLbl.textColor = HexColor("#FFFFFF")
            }

        }
    }
    func resetGaoqing(){
        let btnTag:Int = 101
        for obj in self.subviews{
            if obj.tag >= 100{
                if obj.tag == btnTag{
                    if let tmpBtn = obj as? UIButton{
                        tmpBtn.backgroundColor = HexColor("#D3E4FF")
                        tmpBtn.layer.borderWidth = 0.65
                        tmpBtn.layer.borderColor = HexColor("#478BFF")?.cgColor
                        tmpBtn.isSelected = true
                        for lbl in tmpBtn.subviews{
                            if let tmpLbl = lbl as? UILabel{
                                tmpLbl.textColor = HexColor("#0668E7")
                            }
                        }
                    }
                }else{
                    if let tmpBtn = obj as? UIButton{
                        tmpBtn.backgroundColor = HexColor("#000000")
                        tmpBtn.layer.borderWidth = 0
                        tmpBtn.layer.borderColor = HexColor("#000000")?.cgColor
                        tmpBtn.isSelected = false
                        for lbl in tmpBtn.subviews{
                            if let tmpLbl = lbl as? UILabel{
                                tmpLbl.textColor = HexColor("#FFFFFF")
                            }
                        }
                    }
                }
            }
        }

    }
    @objc func gaoqingBtnAction(btn:UIButton){
        let btnTag = btn.tag
        for obj in self.subviews{
            if obj.tag >= 100{
                if obj.tag == btnTag{
                    if let tmpBtn = obj as? UIButton{
                        tmpBtn.backgroundColor = HexColor("#D3E4FF")
                        tmpBtn.layer.borderWidth = 0.65
                        tmpBtn.layer.borderColor = HexColor("#478BFF")?.cgColor
                        tmpBtn.isSelected = true
                        for lbl in tmpBtn.subviews{
                            if let tmpLbl = lbl as? UILabel{
                                tmpLbl.textColor = HexColor("#0668E7")
                            }
                        }
                    }
                }else{
                    if let tmpBtn = obj as? UIButton{
                        tmpBtn.backgroundColor = HexColor("#000000")
                        tmpBtn.layer.borderWidth = 0
                        tmpBtn.layer.borderColor = HexColor("#000000")?.cgColor
                        tmpBtn.isSelected = false
                        for lbl in tmpBtn.subviews{
                            if let tmpLbl = lbl as? UILabel{
                                tmpLbl.textColor = HexColor("#FFFFFF")
                            }
                        }
                    }
                }
            }
        }
                
        let i = btn.tag-100
        var gaoqing:Int = 3
        if i == 0{
            gaoqing = 2
        }else if i == 1{
            gaoqing = 3
        }else if i == 2{
            gaoqing = 4
        }
        self.gaoqingUpdateBlock?(gaoqing)
    }
    
    @objc func tapAction(){
        self.isHidden = true
    }
    
}