// // BFSubtitleSettingView.swift // BFRecordScreenKit // // Created by ak on 2021/12/7. // 功能:设置字幕操作面板 import BFCommonKit import BFMediaKit import BFUIKit import Foundation import UIKit typealias SubtitleSettingCallBack = (_ subtitileModel: PQEditSubTitleModel) -> Void class BFSubtitleSettingView: UIView { required init?(coder _: NSCoder) { fatalError("init(coder:) has not been implemented") } // 最后一次选择的样式 BTN var lastSelectStyleBtn: UIButton = UIButton() // 最后一次选择的位置 BTN var lastSelectPointBtn: UIButton = UIButton() // 最后一次选择的字号的 BTN var lastSelectwordSizeBtn: UIButton = UIButton() var subtitleSettingCallBack: SubtitleSettingCallBack? var subtitle: PQEditSubTitleModel = PQEditSubTitleModel() // 样式配置 var styleConfig: [Int: [String: Any]] = Dictionary() //操作板背景 let backView = UIButton() override init(frame: CGRect) { super.init(frame: frame) backgroundColor = UIColor.clear addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hidden))) backView.backgroundColor = .black addSubview(backView) backView.snp.makeConstraints { make in make.right.equalTo(self.snp.right) make.bottom.equalTo(self.snp.bottom) make.width.equalTo(cScreenWidth) make.height.equalTo(220) } backView.setNeedsUpdateConstraints() styleConfig = [0: ["fontColor": UIColor.hexColor(hexadecimal: "#FFCF53"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0], 1: ["fontColor": UIColor.hexColor(hexadecimal: "#FF9292"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0], 2: ["fontColor": UIColor.hexColor(hexadecimal: "#80C2FF"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0], 3: ["fontColor": UIColor.hexColor(hexadecimal: "#80E4AB"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0], 4: ["fontColor": UIColor.hexColor(hexadecimal: "#FFFFFF"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0], 5: ["fontColor": UIColor.hexColor(hexadecimal: "#000000"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.white, "backgroundAlpha": 0.0], 6: ["fontColor": UIColor.hexColor(hexadecimal: "#FFFFFF"), "backgroundColor":UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6), "strokeColor": UIColor.clear, "backgroundAlpha": 0.6]] // 字体样式 for i in 0...6 { let btn = UIButton(type: .custom) btn.setImage(imageInRecordScreenKit(by: "wordStyle\(i + 1)"), for: .normal) btn.setBackgroundImage(UIImage(color: .black), for: .normal) btn.frame = CGRect(x: 18 + i * (40 + 10), y: 22, width: 40, height: 40) btn.tag = i if i == 0 { btn.isSelected = true btn.layer.borderColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor btn.layer.borderWidth = 1.5 lastSelectStyleBtn = btn styleSetting(sender: lastSelectStyleBtn) } btn.addTarget(self, action: #selector(styleSetting(sender:)), for: .touchUpInside) backView.addSubview(btn) btn.addCorner(corner: 20.0) } // 位置 let pointTitle = UILabel() pointTitle.textAlignment = .center pointTitle.font = UIFont.systemFont(ofSize: 15) pointTitle.textColor = .white pointTitle.text = "位置" pointTitle.alpha = 0.6 backView.addSubview(pointTitle) pointTitle.snp.makeConstraints { make in make.width.equalTo(40) make.height.equalTo(25) make.left.equalToSuperview().offset(16) make.top.equalToSuperview().offset(89) } for i in 0...2 { let pointBtn = UIButton(type: .custom) pointBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#389AFF"), for: .selected) pointBtn.setTitleColor(.white, for: .normal) pointBtn.addTarget(self, action: #selector(pointSetting(sender:)), for: .touchUpInside) pointBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#1A1A1A") if i == 0 { pointBtn.isSelected = true pointBtn.layer.borderColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor pointBtn.layer.borderWidth = 1.5 lastSelectPointBtn = pointBtn pointBtn.setTitle("下", for: .normal) } else if i == 1 { pointBtn.setTitle("中", for: .normal) } else { pointBtn.setTitle("上", for: .normal) } pointBtn.addCorner(corner: 10) pointBtn.tag = i backView.addSubview(pointBtn) pointBtn.snp.makeConstraints { make in make.width.equalTo(40) make.height.equalTo(30) make.centerX.equalTo(pointTitle.snp.centerX).offset(60 + i * Int(40 + 10)) make.centerY.equalTo(pointTitle.snp.centerY) } } // 字号 let wordSizeTitle = UILabel() wordSizeTitle.textAlignment = .center wordSizeTitle.font = UIFont.systemFont(ofSize: 15) wordSizeTitle.textColor = .white wordSizeTitle.text = "字号" wordSizeTitle.alpha = 0.6 backView.addSubview(wordSizeTitle) wordSizeTitle.snp.makeConstraints { make in make.width.equalTo(40) make.height.equalTo(25) make.right.equalToSuperview().offset(-100) make.top.equalTo(pointTitle.snp.top) } for i in 0...1 { let wordSizeBtn = UIButton(type: .custom) wordSizeBtn.addTarget(self, action: #selector(wordSizeSetting(sender:)), for: .touchUpInside) backView.addSubview(wordSizeBtn) wordSizeBtn.tag = i wordSizeBtn.setBackgroundImage(imageInRecordScreenKit(by: i == 0 ? "wordSizeSetting-_h" : "wordSizeSetting+_h"), for: .normal) wordSizeBtn.snp.makeConstraints { make in make.width.equalTo(24) make.height.equalTo(24) make.left.equalTo(wordSizeTitle.snp.right).offset(10 + i * Int(30 + 10)) make.centerY.equalTo(pointTitle.snp.centerY) } } // 就是一个线 let line = UIView() line.backgroundColor = UIColor.hexColor(hexadecimal: "#121212") backView.addSubview(line) line.snp.makeConstraints { make in make.width.equalTo(340) make.height.equalTo(2) make.left.equalToSuperview().offset(18) make.top.equalTo(wordSizeTitle.snp.bottom).offset(23) } // 不显示字幕 let disabelTitle = UILabel() disabelTitle.textAlignment = .left disabelTitle.font = UIFont.boldSystemFont(ofSize: 17) disabelTitle.textColor = .white disabelTitle.text = "关闭字幕" backView.addSubview(disabelTitle) disabelTitle.snp.makeConstraints { make in make.width.equalTo(100) make.height.equalTo(24) make.left.equalToSuperview().offset(18) make.top.equalTo(line.snp.top).offset(17) } // 创建switch let disabeSwitch = UISwitch() disabeSwitch.isOn = false backView.addSubview(disabeSwitch) disabeSwitch.addTarget(self, action: #selector(switchChange(uiswitch:)), for: .valueChanged) disabeSwitch.snp.makeConstraints { make in make.width.equalTo(51) make.height.equalTo(31) make.right.equalToSuperview().offset(-18) make.top.equalTo(line.snp.top).offset(13) } } override func layoutSubviews() { super.layoutSubviews() self.backView.addCorner(roundingCorners: [.topLeft, .topRight], corner: 10) } @objc func hidden() { isHidden = true } @objc func styleSetting(sender: UIButton) { lastSelectStyleBtn.isSelected = false lastSelectStyleBtn.layer.borderColor = UIColor.clear.cgColor sender.isSelected = true lastSelectStyleBtn = sender sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor sender.layer.borderWidth = 1.5 subtitle.setting.subtitleStyle = sender.tag let config = styleConfig[sender.tag] subtitle.setting.fontColor = config?["fontColor"] as! UIColor subtitle.setting.backgroundColor = config?["backgroundColor"] as! UIColor subtitle.setting.backgroundAlpha = Float(config?["backgroundAlpha"] as! Double) subtitle.setting.strokeColor = config?["strokeColor"] as! UIColor if subtitleSettingCallBack != nil { subtitleSettingCallBack!(subtitle) } } @objc func pointSetting(sender: UIButton) { lastSelectPointBtn.isSelected = false lastSelectPointBtn.layer.borderColor = UIColor.clear.cgColor sender.isSelected = true lastSelectPointBtn = sender sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor sender.layer.borderWidth = 1.5 subtitle.setting.subtitlePoint = sender.tag if subtitleSettingCallBack != nil { subtitleSettingCallBack!(subtitle) } } @objc func wordSizeSetting(sender: UIButton) { if(sender.tag == 0){ if(subtitle.setting.subtitleSize > 20){ subtitle.setting.subtitleSize -= 10 lastSelectwordSizeBtn.setImage(imageInRecordScreenKit(by: "wordSizeSetting+_h"), for: .normal) sender.setImage(imageInRecordScreenKit(by: "wordSizeSetting-_h"), for: .normal) }else{ sender.setImage(imageInRecordScreenKit(by: "wordSizeSetting-_n"), for: .normal) } }else if(sender.tag == 1){ if(subtitle.setting.subtitleSize < 120){ subtitle.setting.subtitleSize += 10 sender.setImage(imageInRecordScreenKit(by: "wordSizeSetting+_h"), for: .normal) lastSelectwordSizeBtn.setImage(imageInRecordScreenKit(by: "wordSizeSetting-_h"), for: .normal) }else{ sender.setImage(imageInRecordScreenKit(by: "wordSizeSetting+_n"), for: .normal) } } lastSelectwordSizeBtn = sender if subtitleSettingCallBack != nil { subtitleSettingCallBack!(subtitle) } } @objc func switchChange(uiswitch: UISwitch) { print("字幕开关状态\(uiswitch.isOn)") subtitle.setting.subtitleIsShow = !uiswitch.isOn if subtitleSettingCallBack != nil { subtitleSettingCallBack!(subtitle) } } }