PQSpeedSettingView.swift 13 KB

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