瀏覽代碼

修改撤销逻辑

harry 3 年之前
父節點
當前提交
911cc00340

+ 36 - 7
BFRecordScreenKit/Classes/BFRecordScreenController.swift

@@ -14,6 +14,13 @@ import BFCommonKit
 import BFFramework
 import UIKit
 
+
+struct WithDrawModel {
+    var type:Int   // 0:拖动; 1:预览播放暂停 2: 录音结束
+    var timestamp:Double
+}
+
+
 public class BFRecordScreenController: BFBaseViewController {
     
     public var nextActionHandle:(()->Void)?
@@ -53,7 +60,10 @@ public class BFRecordScreenController: BFBaseViewController {
     var recordPlayer:AVPlayer? // 录音音频播放器
     var movie :GPUImageMovie?       // 视频预览
     var playView :GPUImageView?     // 视频展示视图
-    var isDragingProgressSlder : Bool = false // 是否在拖动进度条
+    
+    // MARK: 行为参数
+    var events = [WithDrawModel]()              // 行为记录,方便撤销
+    var isDragingProgressSlder : Bool = false   // 是否在拖动进度条
     var isStopAtRecordRange = -1
     //定义音频的编码参数
     let recordSettings:[String : Any] = [AVSampleRateKey : 44100.0, //声音采样率
@@ -197,11 +207,14 @@ public class BFRecordScreenController: BFBaseViewController {
     
     lazy var progressThumV : BFVideoThumbProgressView = {
         let vv = BFVideoThumbProgressView(frame: CGRect(x: 0, y: 54, width: cScreenWidth, height: 50))
-        vv.dragScrollProgressHandle = {[weak self] process in
+        vv.dragScrollProgressHandle = {[weak self] isStart, process in
             DispatchQueue.main.async {[weak self] in
                 guard let sself = self else {
                     return
                 }
+                if isStart {
+                    sself.events.append(WithDrawModel(type: 0, timestamp: sself.currentAssetProgress.seconds))
+                }
                 if sself.isNormalPlaying || sself.isRecording {
                     sself.pause()
                 }
@@ -368,6 +381,8 @@ public class BFRecordScreenController: BFBaseViewController {
         isRecording = true
 
         pause()
+        
+        events.append(WithDrawModel(type: 2, timestamp: self.currentAssetProgress.seconds))
 
         let model = PQVoiceModel()
         model.startTime = self.currentAssetProgress.seconds
@@ -396,17 +411,30 @@ public class BFRecordScreenController: BFBaseViewController {
         
         pause()
     }
+    
     @objc func withdrawAction(){
         pause()
-        if let model = itemModels[currItemModelIndex].voiceStickers.last  {
-            itemModels[currItemModelIndex].voiceStickers.removeLast()
-            drawOrUpdateRecordProgessLable()
+        if let action = events.last {
+            var jumpTime = action.timestamp
+            if action.type == 2 {
+                // 撤销录制
+                if let modelIndex = itemModels[currItemModelIndex].voiceStickers.firstIndex(where: { mod in
+                    mod.startTime == action.timestamp
+                }) {
+                    let model = itemModels[currItemModelIndex].voiceStickers[modelIndex]
+                    itemModels[currItemModelIndex].voiceStickers.remove(at: modelIndex)
+                    drawOrUpdateRecordProgessLable()
+                    jumpTime = model.startTime
+                }
+            }else {
+            }
+            events.removeLast()
             if let dur = itemModels[currItemModelIndex].baseMaterial?.duration.seconds,dur > 0 {
-                changeProgress(progress: Float(model.startTime / dur))
+                changeProgress(progress: Float(jumpTime / dur))
                 isDragingProgressSlder = false
                 currentPlayRecordIndex = -1
                 hadPrepareToPlayRecord = false
-                progressThumV.progress = model.startTime
+                progressThumV.progress = jumpTime
             }
         }
     }
@@ -421,6 +449,7 @@ public class BFRecordScreenController: BFBaseViewController {
             pause()
             searchStopAtRecordRange()
         }else {
+            events.append(WithDrawModel(type: 1, timestamp: self.currentAssetProgress.seconds))
             play()
         }
     }

+ 3 - 3
BFRecordScreenKit/Classes/View/BFVideoThumbProgressView.swift

@@ -60,7 +60,7 @@ class BFVideoThumbProgressView: UIView {
         }
     }
     
-    var dragScrollProgressHandle : ((Float) -> Void)?
+    var dragScrollProgressHandle : ((Bool, Float) -> Void)?
     var dragEndHandle : ((Float) -> Void)?
     var isDrag = false
     
@@ -131,14 +131,14 @@ extension BFVideoThumbProgressView : UIScrollViewDelegate {
     func scrollViewDidScroll(_ scrollView: UIScrollView) {
         if isDrag{
             let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
-            self.dragScrollProgressHandle?(Float(dur))
+            self.dragScrollProgressHandle?(false, Float(dur))
         }
     }
     
     func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
         isDrag = true
         let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
-        self.dragScrollProgressHandle?(Float(dur))
+        self.dragScrollProgressHandle?(true, Float(dur))
     }
     
     func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {