BFIndirectionProgressView.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // BFIndirectionProgressView.swift
  3. // BFRecordScreenKit
  4. //
  5. // Created by SanW on 2021/12/18.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. //
  8. import BFCommonKit
  9. import BFMediaKit
  10. import UIKit
  11. class BFIndirectionProgressView: UIView {
  12. var indirecColor: UIColor = UIColor.clear
  13. var themeColor: UIColor = UIColor.hexColor(hexadecimal: "#28BE67")
  14. var progressHeight: CGFloat = 6
  15. var percenWidth: CGFloat = 0
  16. var totalDuration: Float64 = 0
  17. override private init(frame: CGRect) {
  18. super.init(frame: frame)
  19. }
  20. init(frame: CGRect, indirecColor: UIColor = UIColor.clear, themeColor: UIColor = UIColor.hexColor(hexadecimal: "#28BE67"), percenWidth: CGFloat = 0, progressHeight: CGFloat = 6, totalDuration: Float64 = 0) {
  21. super.init(frame: frame)
  22. self.indirecColor = indirecColor
  23. self.themeColor = themeColor
  24. self.progressHeight = progressHeight
  25. self.percenWidth = percenWidth
  26. if self.percenWidth <= 0, totalDuration > 0 {
  27. self.percenWidth = frame.width / totalDuration
  28. }
  29. }
  30. required init?(coder _: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. func updateProgressViews(items: [PQVoiceModel]) {
  34. subviews.forEach { vv in
  35. vv.removeFromSuperview()
  36. }
  37. items.forEach { model in
  38. createItemView(minX: model.startTime * percenWidth, width: (model.endTime - model.startTime) * percenWidth)
  39. }
  40. }
  41. func setProgress(index: Int, start: CGFloat = 0, progress: Float64) {
  42. if subviews.count > index {
  43. BFLog(message: "设置进度-\(index),\(progress),\(progress * percenWidth)")
  44. subviews[index].frame.size.width = progress * percenWidth
  45. } else {
  46. BFLog(message: "设置进度-添加一个录音:\(index),\(progress),\(start * percenWidth)")
  47. createItemView(minX: start * percenWidth)
  48. }
  49. }
  50. func currentItem(start: CGFloat = 0, progress: Float64) -> UIView {
  51. let newRange = CMTimeRange(start: CMTime(seconds: start, preferredTimescale: 1000), end: CMTime(seconds: start + progress, preferredTimescale: 1000))
  52. var currentIndex: Int?
  53. for (index,item) in subviews.enumerated() {
  54. let originRange = CMTimeRange(start: CMTime(seconds: item.frame.minX / percenWidth, preferredTimescale: 1000), end: CMTime(seconds: item.frame.width / percenWidth, preferredTimescale: 1000))
  55. if CMTimeRangeGetIntersection(originRange, otherRange: newRange).duration.seconds > 0 {
  56. currentIndex = index
  57. // if
  58. break
  59. }
  60. }
  61. if currentIndex != nil {
  62. return subviews[currentIndex!]
  63. } else {
  64. return createItemView(minX: start * percenWidth)
  65. }
  66. }
  67. func deleteItem(index: Int) {
  68. subviews[index].removeFromSuperview()
  69. }
  70. func createItemView(minX: CGFloat, width: CGFloat = 0, indirec: Bool = false) -> UIView{
  71. let lineV = UIView(frame: CGRect(x: minX, y: 0, width: width, height: progressHeight))
  72. lineV.backgroundColor = indirec ? indirecColor : themeColor
  73. lineV.tag = indirec ? 1 : 2
  74. addSubview(lineV)
  75. return lineV
  76. }
  77. }