123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- //
- // 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
- var currentItem: UIView? // 当前的Item
- 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.totalDuration = totalDuration
- 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")
- }
- /// 重绘view
- /// - Parameter items: <#items description#>
- func resetAllSubViews(items: [PQVoiceModel]?, percenWidth: CGFloat = 0, totalDuration: Float64) {
- self.totalDuration = totalDuration
- self.percenWidth = percenWidth
- if self.percenWidth <= 0, totalDuration > 0 {
- self.percenWidth = frame.width / totalDuration
- }
- subviews.forEach { vv in
- vv.removeFromSuperview()
- }
- items?.forEach { model in
- _ = createItemView(minX: model.startTime * percenWidth, width: (model.endTime - model.startTime) * percenWidth)
- }
- }
- /// 设置进度
- /// - Parameters:
- /// - _: <#_ description#>
- /// - start: <#start description#>
- /// - progress: <#progress description#>
- func setProgress(start: CGFloat = 0, progress: Float64) {
- BFLog(message: "录音进度--指示器Indir:progress=\(progress),duration=\(totalDuration),w=\(frame.width),perW=\(percenWidth),totalW:\(progress * percenWidth)")
- detectionAndCreateItem(start: start, progress: progress)
- currentItem?.frame.size.width = progress < 0 ? 0 : progress * percenWidth
- BFLog(message: "当前view:\(String(describing: currentItem))")
- }
- /// 检测并创建item
- /// - Parameter start: <#start description#>
- func detectionAndCreateItem(start: CGFloat = 0, progress: Float64) {
- if currentItem == nil {
- currentItem = detectionItem(start: start, progress: progress)
- }
- }
- /// 检测当前view
- /// - Parameters:
- /// - start: <#start description#>
- /// - progress: <#progress description#>
- /// - Returns: <#description#>
- func detectionItem(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
- break
- }
- }
- if currentIndex != nil {
- BFLog(message: "检测存在重新创建")
- return subviews[currentIndex!]
- } else {
- BFLog(message: "检测不存在重新创建:\(start)")
- return createItemView(minX: start * percenWidth)
- }
- }
- /// 录制结束后重制当前item
- func resetCurrentItem(start: CGFloat, end: CGFloat) {
- currentItem?.frame.origin.x = start * percenWidth
- currentItem?.frame.size.width = (end - start) * percenWidth
- currentItem = nil
- }
- /// 删除某个view
- /// - Parameter index: <#index description#>
- func deleteItem(index: Int = 0) {
- if index >= 0, index < subviews.count {
- subviews[index].removeFromSuperview()
- }
- }
- /// 创建一个view
- /// - Parameters:
- /// - minX: <#minX description#>
- /// - width: <#width description#>
- /// - indirec: <#indirec description#>
- /// - Returns: <#description#>
- func createItemView(minX: CGFloat, width: CGFloat = 0, indirec: Bool = false) -> UIView {
- let lineV = UIView(frame: CGRect(x: minX < 0 ? 0 : minX, y: 0, width: width, height: progressHeight))
- lineV.backgroundColor = indirec ? indirecColor : themeColor
- addSubview(lineV)
- return lineV
- }
- }
|