Browse Source

发音人状态逻辑

jsonwang 3 years ago
parent
commit
51ef216ee6
1 changed files with 116 additions and 80 deletions
  1. 116 80
      BFRecordScreenKit/Classes/RecordScreen/View/BFVoiceSettingView.swift

+ 116 - 80
BFRecordScreenKit/Classes/RecordScreen/View/BFVoiceSettingView.swift

@@ -5,53 +5,51 @@
 //  Created by ak on 2022/2/15.
 //  功能:发音人设置面板
 
-import Foundation
 import BFCommonKit
-import BFUIKit
 import BFMediaKit
+import BFUIKit
+import Foundation
 import UIKit
 
-
 // 操作动作类型
 public enum VoiceSettingActionType: Int {
     case voiceSettingActionCancel = 1 // 取消
     case voiceSettingActionDelete = 2 // 删除
     case VoiceSettingActionConfirm = 3 // 确认
+    case VoiceSettingActionSelected = 4 // 选择了一个发音人 如用于试听
 }
 
 class BFVoiceSettingView: UIView {
-    
     let itemSize = CGSize(width: adapterWidth(width: 52) + 9, height: 40)
-    
-    //操作板背景
+
+    // 操作板背景
     let backView = UIButton()
-    
-    //保存所有发音人
-    public var voices: Dictionary = Dictionary<String, [PQVoiceModel]>.init(){
+
+    // 保存所有发音人
+    public var voices: Dictionary = Dictionary<String, [PQVoiceModel]>.init() {
         didSet {
             voicesCollectView.reloadData()
-            
         }
     }
-    //所有分类
-    public var categorys: Array = Array<BFVoiceCategoryModel>.init(){
+
+    // 所有分类
+    public var categorys: Array = Array<BFVoiceCategoryModel>.init() {
         didSet {
             BFLog(message: "发音人分类数: \(categorys.count)")
             curretnCategory = categorys.first
+            curretnCategory?.isSelected = true
             catagaryCollectView.reloadData()
-        
         }
     }
-    
-    //操作回调
-    public var voiceSettingCallBack: ((_ actionType: VoiceSettingActionType,_ selectVoice:PQVoiceModel?) -> Void)?
 
-    //当前选择的分类
-    var curretnCategory:BFVoiceCategoryModel?
-    //当前选择的发音人
-    var selectVoice:PQVoiceModel?
+    // 操作回调
+    public var voiceSettingCallBack: ((_ actionType: VoiceSettingActionType, _ selectVoice: PQVoiceModel?) -> Void)?
+
+    // 当前选择的分类
+    var curretnCategory: BFVoiceCategoryModel?
+    // 当前选择的发音人
+    public var selectVoice: PQVoiceModel?
 
-    
     // 分类列表
     lazy var catagaryCollectView: UICollectionView = {
         let layout = UICollectionViewFlowLayout()
@@ -78,18 +76,32 @@ class BFVoiceSettingView: UIView {
 
         return collectView
     }()
-    
+
+    // 删除 btn
+    lazy var deleteBtn: UIButton = {
+        let deleteBtn = UIButton()
+        deleteBtn.backgroundColor = .clear
+        deleteBtn.setTitleColor(.white, for: .normal)
+        deleteBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
+        deleteBtn.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside)
+
+        deleteBtn.setBackgroundImage(imageInRecordScreenKit(by: "voiceDeleteN"), for: .normal)
+        deleteBtn.setBackgroundImage(imageInRecordScreenKit(by: "voiceDeleteS"), for: .selected)
+        deleteBtn.adjustsImageWhenHighlighted = false
+        return deleteBtn
+    }()
+
     // 所有发音人列表
     lazy var voicesCollectView: UICollectionView = {
         let layout = UICollectionViewFlowLayout()
         layout.scrollDirection = .vertical
-        layout.itemSize = CGSize(width: adapterWidth(width:60) , height: 78)
-        //每行最小边距
+        layout.itemSize = CGSize(width: adapterWidth(width: 60), height: 78)
+        // 每行最小边距
         layout.minimumLineSpacing = 10
-        //每列最小边距
+        // 每列最小边距
         layout.minimumInteritemSpacing = 0
         layout.sectionInset = UIEdgeInsets.zero
-     
+
         let collectView = UICollectionView(frame: CGRect(x: 0, y: 98, width: cScreenWidth, height: 242 - cSafeAreaHeight), collectionViewLayout: layout)
         collectView.isPagingEnabled = false
         collectView.showsHorizontalScrollIndicator = false
@@ -107,24 +119,20 @@ class BFVoiceSettingView: UIView {
 
         return collectView
     }()
-    
+
     required init?(coder _: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
-    
+
     override func layoutSubviews() {
         super.layoutSubviews()
-        self.backView.addCorner(roundingCorners: [.topLeft, .topRight], corner: 10)
+        backView.addCorner(roundingCorners: [.topLeft, .topRight], corner: 10)
     }
-    
+
     override init(frame: CGRect) {
         super.init(frame: frame)
 
-        backgroundColor = UIColor.clear
-        let view = UIView.init(frame: frame)
-        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hidden)))
-        addSubview(view)
-
+        backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
         backView.backgroundColor = UIColor.hexColor(hexadecimal: "#121212")
 
         addSubview(backView)
@@ -135,8 +143,8 @@ class BFVoiceSettingView: UIView {
             make.height.equalTo(340)
         }
         backView.setNeedsUpdateConstraints()
-        
-        //取消
+
+        // 取消
         let cancelBtn = UIButton()
         cancelBtn.backgroundColor = .clear
         cancelBtn.setTitle("取消", for: .normal)
@@ -144,15 +152,15 @@ class BFVoiceSettingView: UIView {
         cancelBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
         cancelBtn.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
         backView.addSubview(cancelBtn)
-        
+
         cancelBtn.snp.makeConstraints { make in
             make.left.equalToSuperview().offset(18)
             make.width.equalTo(35)
             make.height.equalTo(24)
             make.top.equalToSuperview().offset(18)
         }
-        
-        //确认
+
+        // 确认
         let okBtn = UIButton()
         okBtn.backgroundColor = .clear
         okBtn.setTitle("确认", for: .normal)
@@ -166,9 +174,9 @@ class BFVoiceSettingView: UIView {
             make.height.equalTo(24)
             make.top.equalToSuperview().offset(18)
         }
-        
-        //删除发音设置
-        let catagaryBackView = UIView.init()
+
+        // 删除发音设置
+        let catagaryBackView = UIView()
         catagaryBackView.backgroundColor = UIColor.hexColor(hexadecimal: "#0B0B0B")
         backView.addSubview(catagaryBackView)
         catagaryBackView.snp.makeConstraints { make in
@@ -177,66 +185,90 @@ class BFVoiceSettingView: UIView {
             make.height.equalTo(40)
             make.top.equalToSuperview().offset(58)
         }
-        
+
         catagaryBackView.addSubview(catagaryCollectView)
-        
-        let deleteBtn = UIButton()
-        deleteBtn.backgroundColor = .clear
-        deleteBtn.setTitleColor(.white, for: .normal)
-        deleteBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
-        deleteBtn.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside)
+
         catagaryBackView.addSubview(deleteBtn)
-        deleteBtn.setBackgroundImage(imageInRecordScreenKit(by: "voiceDelete"), for: .normal)
         deleteBtn.snp.makeConstraints { make in
             make.left.equalToSuperview().offset(16)
             make.width.equalTo(24)
             make.height.equalTo(24)
             make.top.equalTo(cancelBtn.bottomY).offset(8)
         }
-  
+
         backView.addSubview(voicesCollectView)
-    
     }
     
+    func show(){
+        isHidden = false
+
+    }
+
     @objc func hidden() {
         isHidden = true
     }
-    
-    @objc func cancelAction(){
-        if(voiceSettingCallBack != nil){
-            voiceSettingCallBack!(.voiceSettingActionCancel,selectVoice)
+
+    @objc func cancelAction() {
+        hidden()
+        selectVoice?.voiceStatue = .isSelected
+        voicesCollectView.reloadData()
+
+        if voiceSettingCallBack != nil {
+            voiceSettingCallBack!(.voiceSettingActionCancel, selectVoice)
         }
     }
     
-    @objc func okBtnAction(){
-        
-        if(voiceSettingCallBack != nil){
-            voiceSettingCallBack!(.VoiceSettingActionConfirm,selectVoice)
+
+    @objc func okBtnAction() {
+        hidden()
+        selectVoice?.voiceStatue = .isSelected
+        voicesCollectView.reloadData()
+        if voiceSettingCallBack != nil {
+            voiceSettingCallBack!(.VoiceSettingActionConfirm, selectVoice)
         }
-        
     }
-    @objc func deleteBtnAction(){
-        if(voiceSettingCallBack != nil){
-            voiceSettingCallBack!(.voiceSettingActionDelete,selectVoice)
+
+    @objc func deleteBtnAction() {
+        // 说明已经选择过发音人
+        if selectVoice != nil {
+            // 删除选择的发音人
+            if deleteBtn.isSelected {
+                selectVoice?.voiceStatue = .isNormal
+                selectVoice = nil
+                voicesCollectView.reloadData()
+            }
+            deleteBtn.isSelected = !deleteBtn.isSelected
+
+        } else {
+            cShowHUB(superView: nil, msg: "还没有选择发音人")
+        }
+
+        if voiceSettingCallBack != nil {
+            voiceSettingCallBack!(.voiceSettingActionDelete, selectVoice)
         }
     }
+    
+    
+    /// 刷新当前选择发音人显示状态
+    /// - Parameter voiceStatue: 状态值
+    public func flushSelectVoiceStatus(voiceStatue: voiceStatue){
+        selectVoice?.voiceStatue = voiceStatue
+        voicesCollectView.reloadData()
+        
+    }
 }
 
 extension BFVoiceSettingView: UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate, UICollectionViewDelegateFlowLayout {
     func collectionView(_ collectionView: UICollectionView,
                         layout _: UICollectionViewLayout,
-                        sizeForItemAt indexPath: IndexPath) -> CGSize
-    {
-      
-        if(collectionView == catagaryCollectView){
+                        sizeForItemAt indexPath: IndexPath) -> CGSize {
+        if collectionView == catagaryCollectView {
             return CGSize(width: adapterWidth(width: 52) + 9, height: 40)
         }
-        return CGSize(width: adapterWidth(width:60) , height: 78)
-        
+        return CGSize(width: adapterWidth(width: 60), height: 78)
     }
 
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection _: Int) -> Int {
-   
         if collectionView == catagaryCollectView {
             return categorys.count
         }
@@ -244,15 +276,13 @@ extension BFVoiceSettingView: UICollectionViewDelegate, UICollectionViewDataSour
     }
 
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- 
         if collectionView == catagaryCollectView {
-
             let cell = BFVoiceCategoryCell.setCategory(collectionView: collectionView, indexPath: indexPath)
- 
+
             cell.category = categorys[indexPath.item]
             return cell
         }
-        
+
         let cell = PQVoiceCell.voiceList(collectionView: collectionView, indexPath: indexPath)
         let model = voices[curretnCategory?.cateName ?? ""]?[indexPath.item]
 
@@ -261,18 +291,24 @@ extension BFVoiceSettingView: UICollectionViewDelegate, UICollectionViewDataSour
     }
 
     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
-     
         if collectionView == catagaryCollectView {
             BFLog(message: "===开始点击\(curretnCategory?.cateName ?? "")")
             curretnCategory?.isSelected = false
             curretnCategory = categorys[indexPath.item]
             curretnCategory?.isSelected = true
             catagaryCollectView.reloadData()
-          
+
             voicesCollectView.reloadData()
-        }else{
+        } else {
+            selectVoice?.voiceStatue = .isNormal
             selectVoice = voices[curretnCategory?.cateName ?? ""]?[indexPath.item]
+            selectVoice?.voiceStatue = .isSelected
+            deleteBtn.isSelected = true
+            voicesCollectView.reloadData()
+
+            if voiceSettingCallBack != nil {
+                voiceSettingCallBack!(.VoiceSettingActionSelected, selectVoice)
+            }
         }
-   
     }
 }