浏览代码

修复搜索的U I

harry 3 年之前
父节点
当前提交
b504e4b162

+ 43 - 5
BFRecordScreenKit/Classes/RecordScreen/Controller/BFMusicSearchController.swift

@@ -21,7 +21,9 @@ class BFMusicSearchController: BFBaseViewController {
     
     var loadedTimeRangesObserver : NSKeyValueObservation?
 
-    var chosedCell : BFMusicInfoSearchCell?
+    var chosedIndexPath : IndexPath?
+
+    var chosedCellStatu : BFMuicInfoCellState = .pause
     
     var searchResults = [PQVoiceModel]()
     
@@ -95,10 +97,11 @@ class BFMusicSearchController: BFBaseViewController {
                 guard let wself = self else { return }
                 
                 guard let ranges = change.newValue as? [CMTimeRange] else { return }
-                if let cell = wself.chosedCell, let totalDur = player.currentItem?.duration, totalDur.isValid,  CMTimeCompare(ranges.first?.duration ?? .zero, totalDur) >= 0{
+                if let indx = wself.chosedIndexPath, let cell = wself.musicTb.cellForRow(at: indx) as? BFMuicInfoCell, let totalDur = player.currentItem?.duration, totalDur.isValid,  CMTimeCompare(ranges.first?.duration ?? .zero, totalDur) >= 0{
                     if (player.currentItem?.asset as? AVURLAsset)?.url.absoluteString ?? "b" == cell.data?.musicPath ?? "a" {
                         if cell.status == .loading{
                             cell.status = .playing
+                            wself.chosedCellStatu = .playing
                         }
                     }
                 }
@@ -108,6 +111,13 @@ class BFMusicSearchController: BFBaseViewController {
         
     }
     
+    func choseCell() -> BFMuicInfoCell? {
+        if let indpx = chosedIndexPath, let cell = musicTb.cellForRow(at: indpx) as? BFMuicInfoCell {
+            return cell
+        }
+        return nil
+    }
+    
     @objc func closeAction(){
         self.dismiss(animated: true, completion: nil)
     }
@@ -143,6 +153,13 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
                     }
                 }
             }
+
+            if cell.data?.isSelected ?? false {
+                chosedIndexPath = indexPath
+                cell.status = chosedCellStatu
+            }else{
+                cell.status = .normal
+            }
         }
         
         cell?.selectionStyle = .none
@@ -157,16 +174,35 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         
         if let cell = tableView.cellForRow(at: indexPath) as? BFMusicInfoSearchCell {
-
+            if chosedIndexPath == indexPath {
+                if cell.status != .pause{
+                    cell.status = .pause
+                    player.pause()
+                }else{
+                    cell.status = .loading
+                    player.play()
+                }
+                return
+            }
             cell.changeSelected(true)
             
-            chosedCell = cell
+            chosedIndexPath = indexPath
             
             if cell.status == .normal {
                 if let urlStr = cell.data?.musicPath, let url = URL(string: urlStr){
                     BFLog(1, message: "歌曲地址: \(url)")
                     if url.absoluteString != (player.currentItem?.asset as? AVURLAsset)?.url.absoluteString ?? "b" {
+                        if player.currentItem != nil {
+                            NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
+                        }
                         player.replaceCurrentItem(with: AVPlayerItem(url: url))
+                        NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) {[weak self] notice in
+                            guard let wself = self else { return }
+                            wself.player.currentItem?.seek(to: .zero, completionHandler: nil)
+                            wself.choseCell()?.status = .pause
+                            wself.chosedCellStatu = .pause
+                        }
+                        
                         cell.status = .loading
                     }else {
                         cell.status = .playing
@@ -179,6 +215,8 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
                 player.pause()
                 cell.status = .pause
             }
+            chosedCellStatu = cell.status
+
         }
     }
     
@@ -187,7 +225,7 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
         cell?.status = .normal
         cell?.changeSelected(false)
         
-        chosedCell = nil
+        chosedIndexPath = nil
         player.pause()
     }
     

+ 21 - 8
BFRecordScreenKit/Classes/RecordScreen/View/BFChooseMusicView.swift

@@ -212,13 +212,6 @@ class BFChooseMusicView: UIView {
         addSubview(progressL)
         
         self.musicVolume = 0.2
-        
-        NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) {[weak self] notice in
-            guard let wself = self else { return }
-            
-            wself.choseCell()?.status = .pause
-            wself.chosedCellStatu = .pause
-        }
 
         loadedTimeRangesObserver = player.observe(\AVPlayer.currentItem?.loadedTimeRanges, options: [.new, .initial]) { [weak self] (player, change) in
             
@@ -418,6 +411,17 @@ extension BFChooseMusicView:UITableViewDelegate, UITableViewDataSource {
     
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         if let cell = tableView.cellForRow(at: indexPath) as? BFMuicInfoCell {
+            if chosedIndexPath == indexPath {
+                if cell.status != .pause{
+                    cell.status = .pause
+                    player.pause()
+                }else{
+                    cell.status = .loading
+                    player.play()
+                }
+                return
+            }
+            
             chosedMusic?.isSelected = false
             chosedMusic = cell.data
             chosedMusic?.isSelected = true
@@ -429,8 +433,17 @@ extension BFChooseMusicView:UITableViewDelegate, UITableViewDataSource {
                 if let urlStr = cell.data?.musicPath, let url = URL(string: urlStr){
                     BFLog(1, message: "歌曲地址: \(url)")
                     if url.absoluteString != (player.currentItem?.asset as? AVURLAsset)?.url.absoluteString ?? "b" {
-                        player.replaceCurrentItem(with: AVPlayerItem(url: url))
                         cell.status = .loading
+                        if player.currentItem != nil {
+                            NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
+                        }
+                        player.replaceCurrentItem(with: AVPlayerItem(url: url))
+                        NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) {[weak self] notice in
+                            guard let wself = self else { return }
+                            wself.player.currentItem?.seek(to: .zero, completionHandler: nil)
+                            wself.choseCell()?.status = .pause
+                            wself.chosedCellStatu = .pause
+                        }
                     }else {
                         cell.status = .playing
                     }

+ 1 - 0
BFRecordScreenKit/Classes/RecordScreen/View/Cell/BFMuicInfoCell.swift

@@ -121,6 +121,7 @@ class BFMuicInfoCell: UITableViewCell {
     }
     
     func changeSelected(_ selected: Bool){
+        data?.isSelected = selected
         if selected{
             titleL.textColor = UIColor.hexColor(hexadecimal: "#389AFF")
             showFunctionBtn()

+ 5 - 3
BFRecordScreenKit/Classes/RecordScreen/View/Cell/BFMusicInfoSearchCell.swift

@@ -18,14 +18,15 @@ class BFMusicInfoSearchCell: BFMuicInfoCell {
         contentView.addSubview(useBtn)
         useBtn.snp.makeConstraints { make in
             make.right.equalTo(-18)
-            make.width.height.equalTo(40)
-            make.top.equalTo(12)
+            make.width.equalTo(46)
+            make.top.equalTo(18)
+            make.bottom.equalTo(-18)
         }
         
         let cutBtn = createCutBtn()
         contentView.addSubview(cutBtn)
         cutBtn.snp.makeConstraints { make in
-            make.right.equalTo(useBtn.snp.left).offset(-10)
+            make.right.equalTo(useBtn.snp.left).offset(-4)
             make.width.height.equalTo(40)
             make.top.equalTo(12)
         }
@@ -44,6 +45,7 @@ class BFMusicInfoSearchCell: BFMuicInfoCell {
         useBtn.setTitle("使用", for: .normal)
         useBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#389AFF")
         useBtn.layer.cornerRadius = 4
+        useBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
         useBtn.addTarget(self, action: #selector(useBtnAction), for: .touchUpInside)
         return useBtn
     }