123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- //
- // 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: "#389AFF")
- var progressHeight: CGFloat = 6
- var tmpPercentW: CGFloat = 0
- var deletedView: UIView?
- var percenWidth: CGFloat {
- get {
- if tmpPercentW.isNaN || tmpPercentW.isInfinite || (tmpPercentW <= 0 && totalDuration > 0) {
- return frame.width / totalDuration
- } else {
- return tmpPercentW
- }
- }
- set {
- tmpPercentW = newValue
- }
- }
- 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: "#389AFF"), 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
- }
- required init?(coder _: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- /// 重绘view
- /// - Parameter items: <#items description#>
- /// - Parameter percenWidth: 图片为固定值要传入,视频的话可以计算,也可以传入
- func resetAllSubViews(items: [PQVoiceModel]?, percenWidth: CGFloat = 0, totalDuration: Float64) {
- frame.size.width = (superview as? UIScrollView)?.contentSize.width ?? 0
- self.totalDuration = totalDuration
- self.percenWidth = percenWidth
- deletedView = nil
- currentItem = nil
- subviews.forEach { vv in
- vv.removeFromSuperview()
- }
- items?.forEach { [weak self] model in
- _ = createItemView(minX: model.startCMTime.seconds * CGFloat(self?.percenWidth ?? 0), width: (model.endCMTime.seconds - model.startCMTime.seconds) * CGFloat(self?.percenWidth ?? 0))
- }
- }
- /// 设置进度
- /// - 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)")
- if start * percenWidth >= frame.width {
- frame.size.width = (superview as? UIScrollView)?.contentSize.width ?? 0
- }
- detectionAndCreateItem(start: start, progress: progress)
- currentItem?.frame.size.width = progress < 0 ? 0 : progress * percenWidth
- BFLog(message: "当前view:\(String(describing: currentItem))")
- let vc = subviews.first { [weak self] sub in
- (self?.currentItem?.frame.minX ?? 0) < sub.frame.minX && sub.frame.minX < (self?.currentItem?.frame.maxX ?? 0)
- }
- if vc != nil {
- deletedView = vc
- vc?.removeFromSuperview()
- BFLog(message: "添加覆盖view-添加:deletedView = \(String(describing: deletedView))")
- }
- }
- /// 检测并创建item
- /// - Parameter start: <#start description#>
- func detectionAndCreateItem(start: CGFloat = 0, progress: Float64) {
- BFLog(message: "检测并创建item:\(start)")
- if currentItem == nil {
- deletedView = 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
- BFLog(message: "重置currentItem:\(start)")
- }
- /// 删除某个view
- /// - Parameter index: <#index description#>
- func deleteItem(index: Int = 0, isCurrent: Bool = false, isImage _: Bool = false) {
- BFLog(message: "添加覆盖view-不足一秒:\(isCurrent),deletedView = \(String(describing: deletedView))")
- if isCurrent {
- currentItem?.removeFromSuperview()
- currentItem = nil
- if deletedView != nil {
- BFLog(message: "添加view")
- addSubview(deletedView!)
- }
- } else {
- if index >= 0, index < subviews.count {
- subviews[index].removeFromSuperview()
- }
- }
- deletedView = nil
- }
- /// 创建一个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
- }
- }
|