12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- //
- // BFIndirectionProgressView.swift
- // BFRecordScreenKit
- //
- // Created by SanW on 2021/12/18.
- // Copyright © 2021 BytesFlow. All rights reserved.
- //
- import BFCommonKit
- import BFMediaKit
- import UIKit
- class BFIndirectionProgressView: UIView {
- var indirecColor: UIColor = UIColor.clear
- var themeColor: UIColor = UIColor.hexColor(hexadecimal: "#28BE67")
- var progressHeight: CGFloat = 6
- var percenWidth: CGFloat = 0
- var totalDuration: Float64 = 0
- override private init(frame: CGRect) {
- super.init(frame: frame)
- }
- init(frame: CGRect, indirecColor: UIColor = UIColor.clear, themeColor: UIColor = UIColor.hexColor(hexadecimal: "#28BE67"), percenWidth: CGFloat = 0, progressHeight: CGFloat = 6, totalDuration: Float64 = 0) {
- super.init(frame: frame)
- self.indirecColor = indirecColor
- self.themeColor = themeColor
- self.progressHeight = progressHeight
- self.percenWidth = percenWidth
- if self.percenWidth <= 0, totalDuration > 0 {
- self.percenWidth = frame.width / totalDuration
- }
- }
- required init?(coder _: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- func updateProgressViews(items: [PQVoiceModel]) {
- subviews.forEach { vv in
- vv.removeFromSuperview()
- }
- items.forEach { model in
- createItemView(minX: model.startTime * percenWidth, width: (model.endTime - model.startTime) * percenWidth)
- }
- }
- func setProgress(index: Int, start: CGFloat = 0, progress: Float64) {
- if subviews.count > index {
- BFLog(message: "设置进度-\(index),\(progress),\(progress * percenWidth)")
- subviews[index].frame.size.width = progress * percenWidth
- } else {
- BFLog(message: "设置进度-添加一个录音:\(index),\(progress),\(start * percenWidth)")
- createItemView(minX: start * percenWidth)
- }
-
- }
- func currentItem(start: CGFloat = 0, progress: Float64) -> UIView {
- let newRange = CMTimeRange(start: CMTime(seconds: start, preferredTimescale: 1000), end: CMTime(seconds: start + progress, preferredTimescale: 1000))
- var currentIndex: Int?
- for (index,item) in subviews.enumerated() {
- let originRange = CMTimeRange(start: CMTime(seconds: item.frame.minX / percenWidth, preferredTimescale: 1000), end: CMTime(seconds: item.frame.width / percenWidth, preferredTimescale: 1000))
- if CMTimeRangeGetIntersection(originRange, otherRange: newRange).duration.seconds > 0 {
- currentIndex = index
- // if
- break
- }
- }
- if currentIndex != nil {
- return subviews[currentIndex!]
- } else {
- return createItemView(minX: start * percenWidth)
- }
- }
-
- func deleteItem(index: Int) {
- subviews[index].removeFromSuperview()
- }
- func createItemView(minX: CGFloat, width: CGFloat = 0, indirec: Bool = false) -> UIView{
- let lineV = UIView(frame: CGRect(x: minX, y: 0, width: width, height: progressHeight))
- lineV.backgroundColor = indirec ? indirecColor : themeColor
- lineV.tag = indirec ? 1 : 2
- addSubview(lineV)
- return lineV
- }
- }
|