PQSpeedSettingView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. ["6.0x\n1.2x",
  49. "5.0x\n1.0x",
  50. "3.0x\n0.5x",
  51. "2.4x\n0.4x",
  52. "1.8x\n0.3x",
  53. "1.0x\n0.2x",
  54. "自定义\n快慢速"]
  55. let tempMaxSpeed = [6, 5, 3, 2.4, 1.8, 1.0, 0.0]
  56. let tempMinSpeed = [1.2, 1.0, 0.5, 0.4, 0.3, 0.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 && lastSelectModel != nil{
  83. datas.insert(insertModle!, at: 5)
  84. }
  85. titleCollectionView.reloadData()
  86. }
  87. }
  88. //上一次插入的倍速数据
  89. var insertModle:PQSpeedTitleModel?
  90. // 点击回调 maxSpeed,minSpeed 同时为0 说明点击的是自定义速度
  91. public var selectSpeedCallBack: ((_ maxSpeed: Float, _ minSpeed: Float,_ selectIndex:Int,_ isSettingPlayer:Bool) -> Void)?
  92. override init(frame: CGRect) {
  93. super.init(frame: frame)
  94. addSubview(titleCollectionView)
  95. }
  96. required init?(coder _: NSCoder) {
  97. fatalError("init(coder:) has not been implemented")
  98. }
  99. //设置默认选择的
  100. /// - Parameters:
  101. /// - index: index: 第几位 从0 开始
  102. /// - isSettingPlayer: 是否重启播放器
  103. /// - setEnable: 设置不可用状态
  104. func setSelectItem(index:Int,isSettingPlayer:Bool = true,setDisable:Bool = false,isCancle:Bool = false) {
  105. BFLog(message: "setSelectItem is \(index)")
  106. if(index < 0){
  107. BFLog(message: "选择位置数据出错\(index)")
  108. return
  109. }
  110. //设置不可用状态
  111. if(setDisable){
  112. for (i,model) in datas.enumerated() {
  113. model.isDisable = i == index ? false: true
  114. }
  115. }
  116. lastSelectModel?.isSelected = false
  117. // && index < datas.count && (datas[index].title != "自定义" || datas[index].title != "自定义\n快慢速"))
  118. if viewType == 3{
  119. if(index > 4 && datas.count < 7){
  120. if(index >= datas.count){
  121. let model = PQSpeedTitleModel()
  122. model.title = "\(index + 1)x"
  123. model.maxSpeed = Float(index + 1)
  124. insertModle = model
  125. datas.insert(insertModle!, at: 5)
  126. lastSelectModel = datas[5]
  127. }else{
  128. lastSelectModel = datas[index]
  129. }
  130. }else{
  131. lastSelectModel = datas[index]
  132. }
  133. }else{
  134. lastSelectModel = datas[index]
  135. }
  136. lastSelectModel?.isSelected = true
  137. titleCollectionView.reloadData()
  138. //发出回调,调用方走统一处理逻辑
  139. if selectSpeedCallBack != nil {
  140. BFLog(message: "选择的速度为 max: \(lastSelectModel?.maxSpeed ?? 0.0) min: \(lastSelectModel?.minSpeed ?? 0.0) title \(lastSelectModel?.title ?? "")")
  141. let lastSelectIndex = datas.firstIndex(where: { (model) -> Bool in
  142. (model.maxSpeed == lastSelectModel?.maxSpeed)
  143. }) ?? 0
  144. selectSpeedCallBack!(lastSelectModel?.maxSpeed ?? 0.0, lastSelectModel?.minSpeed ?? 0.0,lastSelectIndex,isSettingPlayer)
  145. }
  146. }
  147. //选中自定义
  148. func selectCustom() {
  149. lastSelectModel?.isSelected = false
  150. lastSelectModel = datas.last
  151. lastSelectModel?.isSelected = true
  152. titleCollectionView.reloadData()
  153. }
  154. }
  155. extension PQSpeedSettingView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  156. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  157. return datas.count
  158. }
  159. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  160. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQSpeedTitleCell.self), for: indexPath) as! PQSpeedTitleCell
  161. cell.titleModel = datas[indexPath.row]
  162. return cell
  163. }
  164. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  165. BFLog(message: "选择了 \(String(describing: datas[indexPath.row]))")
  166. if(datas[indexPath.row].isDisable){
  167. BFLog(message: "不可用速度")
  168. cShowHUB(superView: nil, msg: "素材时长需要大于6秒\n 才可选择其他档位")
  169. return
  170. }
  171. if(datas[indexPath.row].title == "自定义" || datas[indexPath.row].title == "自定义\n快慢速"){
  172. selectSpeedCallBack!(-1,-1,indexPath.row, false)
  173. }else{
  174. setSelectItem(index: indexPath.row)
  175. }
  176. //下面只是统计 //1, 快慢速度卡点 2,跳跃卡点 ,3,循环设置
  177. if(viewType == 1){
  178. if(lastSelectModel?.title == "自定义"){
  179. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_customizeSpeed, pageSource: .sp_stuck_previewSyncedUp, extParams:nil, remindmsg: "")
  180. }else{
  181. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectSpeed, pageSource: .sp_stuck_previewSyncedUp, extParams:nil, remindmsg: "")
  182. }
  183. }else if(viewType == 2){
  184. if(lastSelectModel?.title == "自定义"){
  185. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_customizeRatio, pageSource: .sp_stuck_previewSyncedUp, extParams:nil, remindmsg: "")
  186. }else{
  187. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectRatio, pageSource: .sp_stuck_previewSyncedUp, extParams:nil, remindmsg: "")
  188. }
  189. }else if(viewType == 3){
  190. if(lastSelectModel?.title == "自定义"){
  191. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_customizeRepeatTimes, pageSource: .sp_stuck_previewSyncedUp, extParams:nil, remindmsg: "")
  192. }else{
  193. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_selectRepeatTimes, pageSource: .sp_stuck_previewSyncedUp, extParams:nil, remindmsg: "")
  194. }
  195. }
  196. }
  197. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  198. // 20 是 cell label 上下边距总和
  199. if viewType == 1 {
  200. if indexPath.row == datas.count - 1 {
  201. return CGSize(width: 65, height: 44)
  202. }
  203. return CGSize(width: 44 + 10, height: 24 + 20)
  204. } else {
  205. if indexPath.row == 0 || indexPath.row == datas.count - 1 {
  206. return CGSize(width: 60 + 10, height: 30)
  207. }
  208. return CGSize(width: 30 + 10, height: 30)
  209. }
  210. }
  211. }
  212. class PQSpeedTitleModel: NSObject {
  213. // UI 上显示的文字
  214. var title: String = ""
  215. // 是否已经选择
  216. var isSelected: Bool = false
  217. // 最大、最小速度
  218. var maxSpeed: Float = 0.0
  219. var minSpeed: Float = 0.0
  220. //是否可用
  221. var isDisable:Bool = false
  222. public override init() {
  223. super.init()
  224. }
  225. }
  226. class PQSpeedTitleCell: UICollectionViewCell {
  227. lazy var titleLab: UILabel = {
  228. let titleLab = UILabel()
  229. titleLab.font = UIFont.systemFont(ofSize: 13, weight: .regular)
  230. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  231. titleLab.numberOfLines = 0
  232. titleLab.lineBreakMode = .byCharWrapping
  233. titleLab.isUserInteractionEnabled = true
  234. titleLab.textAlignment = .center
  235. titleLab.addCorner(corner: 5)
  236. return titleLab
  237. }()
  238. override init(frame: CGRect) {
  239. super.init(frame: frame)
  240. contentView.addSubview(titleLab)
  241. titleLab.snp.remakeConstraints { make in
  242. make.height.equalToSuperview()
  243. make.width.equalToSuperview().offset(-10)
  244. make.left.equalToSuperview()
  245. make.top.equalToSuperview()
  246. }
  247. }
  248. required init?(coder _: NSCoder) {
  249. fatalError("init(coder:) has not been implemented")
  250. }
  251. var titleModel: PQSpeedTitleModel? {
  252. didSet {
  253. titleLab.text = titleModel?.title
  254. titleLab.snp.remakeConstraints { make in
  255. make.height.equalToSuperview()
  256. make.width.equalToSuperview().offset(-10)
  257. make.left.equalToSuperview()
  258. make.top.equalToSuperview()
  259. }
  260. if titleModel?.isSelected ?? false {
  261. titleLab.backgroundColor = UIColor(red: 0.24, green: 0.758, blue: 0.758, alpha: 0.15)
  262. titleLab.textColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  263. } else {
  264. if(titleModel?.isDisable ?? false){
  265. titleLab.backgroundColor = .clear
  266. titleLab.textColor = UIColor.hexColor(hexadecimal: "#DFDFDF")
  267. }else{
  268. titleLab.backgroundColor = UIColor.hexColor(hexadecimal: "#F9F9F9")
  269. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  270. }
  271. }
  272. }
  273. }
  274. }