PQSpeedSettingView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //
  2. // PQSpeedSettingView.swift
  3. // BFFramework
  4. //
  5. // Created by ak on 2021/8/2.
  6. // 功能:设置快慢速 跳越卡点 的倍速 VIEW
  7. import Foundation
  8. import BFCommonKit
  9. import BFAnalyzeKit
  10. class PQSpeedSettingView: UIView {
  11. // 速度列表
  12. lazy var titleCollectionView: UICollectionView = {
  13. let flowLayout = UICollectionViewFlowLayout()
  14. flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  15. flowLayout.minimumLineSpacing = 0
  16. flowLayout.minimumInteritemSpacing = 0
  17. flowLayout.scrollDirection = .horizontal
  18. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
  19. collectionView.showsVerticalScrollIndicator = false
  20. collectionView.showsHorizontalScrollIndicator = false
  21. collectionView.delegate = self
  22. collectionView.dataSource = self
  23. collectionView.backgroundColor = .clear
  24. collectionView.register(PQSpeedTitleCell.self, forCellWithReuseIdentifier: String(describing: PQSpeedTitleCell.self))
  25. if #available(iOS 11.0, *) {
  26. collectionView.contentInsetAdjustmentBehavior = .never
  27. }
  28. // 延迟scrollView上子视图的响应,所以当直接拖动UISlider时,如果此时touch时间在150ms以内,UIScrollView会认为是拖动自己,从而拦截了event,导致UISlider接收不到滑动的event
  29. collectionView.delaysContentTouches = false
  30. return collectionView
  31. }()
  32. // 保存数据
  33. var datas: Array<PQSpeedTitleModel> = Array()
  34. var lastSelectModel: PQSpeedTitleModel?
  35. // view 初化的类型 1, 快慢速度卡点 2,跳跃卡点 ,3,循环设置
  36. var viewType: Int = 0 {
  37. didSet {
  38. if(viewType == oldValue){
  39. return
  40. }
  41. titleCollectionView.snp.remakeConstraints { make in
  42. make.right.equalToSuperview()
  43. make.width.equalToSuperview()
  44. make.height.equalTo(viewType == 1 ? 44 : 30)
  45. make.top.equalToSuperview()
  46. }
  47. datas.removeAll()
  48. if viewType == 1 {
  49. let tempTitle =
  50. ["1.0x\n0.2x",
  51. "1.8x\n0.3x",
  52. "2.4x\n0.4x",
  53. "3.0x\n0.5x",
  54. "5.0x\n1.0x",
  55. "6.0x\n1.2x",
  56. "自定义\n快慢速"]
  57. let tempMaxSpeed = [1.0, 1.8, 2.4, 3, 5, 6, 0.0]
  58. let tempMinSpeed = [0.2, 0.3, 0.4,0.5,1.0,1.2, 0.0]
  59. for (index, str) in tempTitle.enumerated() {
  60. let model = PQSpeedTitleModel()
  61. model.title = str
  62. model.maxSpeed = Float(tempMaxSpeed[index])
  63. model.minSpeed = Float(tempMinSpeed[index])
  64. datas.append(model)
  65. }
  66. } else {
  67. let str:String = (viewType == 2) ? "跳跃" : "循环"
  68. let tempTitle =
  69. ["\(str)1x",
  70. "2x",
  71. "3x",
  72. "4x",
  73. "5x",
  74. "自定义"]
  75. let tempMaxSpeed = [1, 2, 3, 4, 5, 0]
  76. for (index, str) in tempTitle.enumerated() {
  77. let model = PQSpeedTitleModel()
  78. model.title = str
  79. model.maxSpeed = Float(tempMaxSpeed[index])
  80. datas.append(model)
  81. }
  82. }
  83. //如果有老数据先插入 补位
  84. if viewType == 3 && insertModle != nil{
  85. datas.insert(insertModle!, at: 5)
  86. }
  87. if(lastSelectModel != nil){
  88. selectCustom()
  89. }else{
  90. titleCollectionView.reloadData()
  91. }
  92. }
  93. }
  94. //上一次插入的倍速数据
  95. var insertModle:PQSpeedTitleModel?
  96. // 点击回调 maxSpeed,minSpeed 同时为0 说明点击的是自定义速度
  97. public var selectSpeedCallBack: ((_ maxSpeed: Float, _ minSpeed: Float,_ selectIndex:Int,_ isSettingPlayer:Bool) -> Void)?
  98. override init(frame: CGRect) {
  99. super.init(frame: frame)
  100. addSubview(titleCollectionView)
  101. }
  102. required init?(coder _: NSCoder) {
  103. fatalError("init(coder:) has not been implemented")
  104. }
  105. deinit {
  106. BFLog(1, message: "speed setting view release")
  107. }
  108. //设置默认选择的
  109. /// - Parameters:
  110. /// - index: index: 第几位 从0 开始
  111. /// - isSettingPlayer: 是否重启播放器
  112. /// - setEnable: 设置不可用状态
  113. /// - enableInsert: 是否插入一组新数据,只有第一次 < 10s 设置为 true ,自定义及其它情况都不设置 true
  114. func setSelectItem(index:Int,isSettingPlayer:Bool = true,setDisable:Bool = false,isCancle:Bool = false,enableInsert:Bool = false) {
  115. BFLog(message: "setSelectItem is \(index)")
  116. if(index < 0 ){
  117. BFLog(message: "选择位置数据出错\(index)")
  118. return
  119. }
  120. //设置不可用状态
  121. if(setDisable){
  122. for (i,model) in datas.enumerated() {
  123. model.isDisable = i == index ? false: true
  124. }
  125. }
  126. lastSelectModel?.isSelected = false
  127. if viewType == 3{
  128. if(index > 4 && datas.count < 7 && enableInsert){
  129. if(index >= datas.count - 1){
  130. let model = PQSpeedTitleModel()
  131. model.title = "\(index + 1)x"
  132. model.maxSpeed = Float(index + 1)
  133. insertModle = model
  134. datas.insert(insertModle!, at: 5)
  135. lastSelectModel = datas[5]
  136. }else{
  137. lastSelectModel = datas[index]
  138. }
  139. }else{
  140. if(index > datas.count){
  141. lastSelectModel?.isSelected = true
  142. titleCollectionView.reloadData()
  143. return
  144. }
  145. lastSelectModel = datas[index]
  146. }
  147. }else{
  148. lastSelectModel = datas[index]
  149. }
  150. lastSelectModel?.isSelected = true
  151. titleCollectionView.reloadData()
  152. //发出回调,调用方走统一处理逻辑
  153. if selectSpeedCallBack != nil {
  154. BFLog(message: "选择的速度为 max: \(lastSelectModel?.maxSpeed ?? 0.0) min: \(lastSelectModel?.minSpeed ?? 0.0) title \(lastSelectModel?.title ?? "")")
  155. let lastSelectIndex = datas.firstIndex(where: { (model) -> Bool in
  156. (model.maxSpeed == lastSelectModel?.maxSpeed)
  157. }) ?? 0
  158. selectSpeedCallBack!(lastSelectModel?.maxSpeed ?? 0.0, lastSelectModel?.minSpeed ?? 0.0,lastSelectIndex,isSettingPlayer)
  159. }
  160. }
  161. //选中自定义
  162. func selectCustom() {
  163. lastSelectModel?.isSelected = false
  164. lastSelectModel = datas.last
  165. lastSelectModel?.isSelected = true
  166. titleCollectionView.reloadData()
  167. }
  168. }
  169. extension PQSpeedSettingView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  170. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  171. return datas.count
  172. }
  173. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  174. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQSpeedTitleCell.self), for: indexPath) as! PQSpeedTitleCell
  175. cell.titleModel = datas[indexPath.row]
  176. return cell
  177. }
  178. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  179. BFLog(message: "选择了 \(String(describing: datas[indexPath.row]))")
  180. if(datas[indexPath.row].isDisable){
  181. BFLog(message: "不可用速度")
  182. cShowHUB(superView: nil, msg: "素材时长需要大于6秒\n 才可选择其他档位")
  183. return
  184. }
  185. if(datas[indexPath.row].title == "自定义" || datas[indexPath.row].title == "自定义\n快慢速"){
  186. selectSpeedCallBack!(-1,-1,indexPath.row, false)
  187. }else{
  188. setSelectItem(index: indexPath.row)
  189. }
  190. //下面只是统计 //1, 快慢速度卡点 2,跳跃卡点 ,3,循环设置
  191. if(viewType == 1){
  192. if(datas[indexPath.row].title == "自定义" || datas[indexPath.row].title == "自定义\n快慢速"){
  193. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_customizeSpeed, pageSource: .sp_stuck_previewSyncedUp,commonParams: commonParams())
  194. }else{
  195. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectSpeed, pageSource: .sp_stuck_previewSyncedUp,commonParams: commonParams())
  196. }
  197. }else if(viewType == 2){
  198. if(datas[indexPath.row].title == "自定义" || datas[indexPath.row].title == "自定义\n快慢速"){
  199. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_customizeRatio, pageSource: .sp_stuck_previewSyncedUp,commonParams: commonParams())
  200. }else{
  201. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectRatio, pageSource: .sp_stuck_previewSyncedUp,commonParams: commonParams())
  202. }
  203. }else if(viewType == 3){
  204. if(datas[indexPath.row].title == "自定义" || datas[indexPath.row].title == "自定义\n快慢速"){
  205. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_customizeRepeatTimes, pageSource: .sp_stuck_previewSyncedUp,commonParams: commonParams())
  206. }else{
  207. BFEventTrackAdaptor.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectRepeatTimes, pageSource: .sp_stuck_previewSyncedUp,commonParams: commonParams())
  208. }
  209. }
  210. }
  211. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  212. // 20 是 cell label 上下边距总和
  213. if viewType == 1 {
  214. if indexPath.row == datas.count - 1 {
  215. return CGSize(width: 65, height: 44)
  216. }
  217. return CGSize(width: 44 + 10, height: 24 + 20)
  218. } else {
  219. if indexPath.row == 0 || indexPath.row == datas.count - 1 {
  220. return CGSize(width: 60 + 10, height: 30)
  221. }
  222. return CGSize(width: 30 + 10, height: 30)
  223. }
  224. }
  225. }
  226. class PQSpeedTitleModel: NSObject {
  227. // UI 上显示的文字
  228. var title: String = ""
  229. // 是否已经选择
  230. var isSelected: Bool = false
  231. // 最大、最小速度
  232. var maxSpeed: Float = 0.0
  233. var minSpeed: Float = 0.0
  234. //是否可用
  235. var isDisable:Bool = false
  236. public override init() {
  237. super.init()
  238. }
  239. }
  240. class PQSpeedTitleCell: UICollectionViewCell {
  241. lazy var titleLab: UILabel = {
  242. let titleLab = UILabel()
  243. titleLab.font = UIFont.systemFont(ofSize: 13, weight: .regular)
  244. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  245. titleLab.numberOfLines = 0
  246. titleLab.lineBreakMode = .byCharWrapping
  247. titleLab.isUserInteractionEnabled = true
  248. titleLab.textAlignment = .center
  249. titleLab.addCorner(corner: 5)
  250. return titleLab
  251. }()
  252. override init(frame: CGRect) {
  253. super.init(frame: frame)
  254. contentView.addSubview(titleLab)
  255. titleLab.snp.remakeConstraints { make in
  256. make.height.equalToSuperview()
  257. make.width.equalToSuperview().offset(-10)
  258. make.left.equalToSuperview()
  259. make.top.equalToSuperview()
  260. }
  261. }
  262. required init?(coder _: NSCoder) {
  263. fatalError("init(coder:) has not been implemented")
  264. }
  265. var titleModel: PQSpeedTitleModel? {
  266. didSet {
  267. titleLab.text = titleModel?.title
  268. titleLab.snp.remakeConstraints { make in
  269. make.height.equalToSuperview()
  270. make.width.equalToSuperview().offset(-10)
  271. make.left.equalToSuperview()
  272. make.top.equalToSuperview()
  273. }
  274. if titleModel?.isSelected ?? false {
  275. let styleColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)
  276. titleLab.backgroundColor = UIColor(red: styleColor.rgbaf[0], green: styleColor.rgbaf[1], blue: styleColor.rgbaf[2], alpha: 0.15)
  277. titleLab.textColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)
  278. titleLab.font = UIFont.boldSystemFont(ofSize: 13)
  279. } else {
  280. if(titleModel?.isDisable ?? false){
  281. let backColor = BFConfig.shared.pointEditNamalBackgroundColor
  282. let textColor = UIColor.hexColor(hexadecimal: "#959595")
  283. titleLab.backgroundColor = UIColor.init(red: backColor.rgbaf[0], green: backColor.rgbaf[1], blue: backColor.rgbaf[2], alpha: 0.3)
  284. titleLab.textColor = UIColor.init(red: textColor.rgbaf[0], green: textColor.rgbaf[1], blue: textColor.rgbaf[2], alpha: 0.3)
  285. }else{
  286. titleLab.backgroundColor = BFConfig.shared.pointEditNamalBackgroundColor
  287. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  288. }
  289. titleLab.font = UIFont.systemFont(ofSize: 13, weight: .regular)
  290. }
  291. }
  292. }
  293. }