CNLiveVodLandscapeBeiSuView.swift 7.49 KB
//
//  CNLiveVodLandscapeBeiSuView.swift
//  metaCode
//
//  Created by zx on 2025/3/27.
//  横屏倍速

import Foundation

typealias CNLiveVodLandscapeBeiSuUpdateBlock = (_ rate:Float)->Void

class CNLiveVodLandscapeBeiSuView:UIView,UIGestureRecognizerDelegate{
    
    var beiSuUpdateBlock:CNLiveVodLandscapeBeiSuUpdateBlock?
    var currSeleIdx = 2//1.0
    //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)))
    }
    
    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 W:CGFloat = 65.0
        var H:CGFloat = 65.0
        let padding:CGFloat = 18.0
        let leftPadding:CGFloat = (KSreenHeight-W*6-padding*5)*0.5
        var title = "0.5x"
        for i in 0..<6{
            let expandBtn = UIButton.init(type: .custom)
            expandBtn.frame = CGRect(x: leftPadding+CGFloat(i)*(W+padding), y: 0, width: W, height: H)
            expandBtn.centerY = KSreenWidth*0.5
            if i == 0{
                title = "0.5x"
            }else if i == 1{
                title = "0.75x"
            }else if i == 2{
                title = "1.0x"
                
            }else if i == 3{
                title = "1.25x"
            }else if i == 4{
                title = "1.5x"
            }else if i == 5{
                title = "2.0x"
            }
            if i == self.currSeleIdx{
                expandBtn.backgroundColor = HexColor("#D3E4FF")
                expandBtn.layer.borderWidth = 0.65
                expandBtn.layer.borderColor = HexColor("#478BFF")?.cgColor
                expandBtn.isSelected = true
            }else{
                expandBtn.backgroundColor = HexColor("#000000")
                expandBtn.layer.borderWidth = 0
                expandBtn.layer.borderColor = HexColor("#000000")?.cgColor
                expandBtn.isSelected = false
            }
            expandBtn.setTitle(title, for: .normal)
            expandBtn.setTitle(title, 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(beisuBtnAction), for: .touchUpInside)
            self.addSubview(expandBtn)
        }
        
        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")
    }
    
    @objc func beisuBtnAction(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
                    }
                }else{
                    if let tmpBtn = obj as? UIButton{
                        tmpBtn.backgroundColor = HexColor("#000000")
                        tmpBtn.layer.borderWidth = 0
                        tmpBtn.layer.borderColor = HexColor("#000000")?.cgColor
                        tmpBtn.isSelected = false
                    }
                }
            }
        }
                
        let i = btn.tag-100
        var rate:Float = 1.0
        if i == 0{
            rate = 0.5
        }else if i == 1{
            rate = 0.75
        }else if i == 2{
            rate = 1.0
        }else if i == 3{
            rate = 1.25
        }else if i == 4{
            rate = 1.5
        }else if i == 5{
            rate = 2.0
        }
        self.beiSuUpdateBlock?(rate)
    }
    
    @objc func tapAction(){
        self.isHidden = true
    }
    
    //恢复1倍速
    func setDefault1Beisu(){
        let btnTag = 102
        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
                    }
                }else{
                    if let tmpBtn = obj as? UIButton{
                        tmpBtn.backgroundColor = HexColor("#000000")
                        tmpBtn.layer.borderWidth = 0
                        tmpBtn.layer.borderColor = HexColor("#000000")?.cgColor
                        tmpBtn.isSelected = false
                    }
                }
            }
        }
    }
}