PQSpeedSettingView.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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,跳跃卡点
  34. var viewType: Int = 0 {
  35. didSet {
  36. titleCollectionView.snp.remakeConstraints { make in
  37. make.right.equalToSuperview()
  38. make.width.equalToSuperview()
  39. make.height.equalTo(viewType == 1 ? 44 : 30)
  40. make.top.equalToSuperview()
  41. }
  42. datas.removeAll()
  43. if viewType == 1 {
  44. let tempTitle =
  45. ["6.0x\n1.2x",
  46. "5.0x\n1.0x",
  47. "3.0x\n0.5x",
  48. "2.4x\n0.4x",
  49. "1.0x\n0.3x",
  50. "1.0x\n0.2x",
  51. "自定义\n快慢速"]
  52. let tempMaxSpeed = [6, 5, 4, 3, 2.4, 1.0, 0.0]
  53. let tempMinSpeed = [1.2, 1.0, 0.5, 0.4, 0.3, 0.2, 0.0]
  54. for (index, str) in tempTitle.enumerated() {
  55. let model = PQSpeedTitleModel()
  56. model.title = str
  57. model.maxSpeed = Float(tempMaxSpeed[index])
  58. model.minSpeed = Float(tempMinSpeed[index])
  59. datas.append(model)
  60. }
  61. } else {
  62. let tempTitle =
  63. ["跳跃1x",
  64. "2x",
  65. "3x",
  66. "4x",
  67. "5x",
  68. "自定义"]
  69. let tempMaxSpeed = [1, 2, 3, 4, 5, 0]
  70. for (index, str) in tempTitle.enumerated() {
  71. let model = PQSpeedTitleModel()
  72. model.title = str
  73. model.maxSpeed = Float(tempMaxSpeed[index])
  74. datas.append(model)
  75. }
  76. }
  77. titleCollectionView.reloadData()
  78. }
  79. }
  80. // 点击回调 maxSpeed,minSpeed 同时为0 说明点击的是自定义速度
  81. public var selectSpeedCallBack: ((_ maxSpeed: Float, _ minSpeed: Float) -> Void)?
  82. override init(frame: CGRect) {
  83. super.init(frame: frame)
  84. addSubview(titleCollectionView)
  85. }
  86. required init?(coder _: NSCoder) {
  87. fatalError("init(coder:) has not been implemented")
  88. }
  89. }
  90. extension PQSpeedSettingView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  91. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  92. return datas.count
  93. }
  94. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  95. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQSpeedTitleCell.self), for: indexPath) as! PQSpeedTitleCell
  96. cell.titleModel = datas[indexPath.row]
  97. return cell
  98. }
  99. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  100. BFLog(message: "选择了 \(String(describing: datas[indexPath.row]))")
  101. lastSelectModel?.isSelected = false
  102. datas[indexPath.row].isSelected = true
  103. titleCollectionView.reloadData()
  104. lastSelectModel = datas[indexPath.row]
  105. if selectSpeedCallBack != nil {
  106. BFLog(message: "选择的速度为 max: \(lastSelectModel?.maxSpeed ?? 0.0) min: \(lastSelectModel?.minSpeed ?? 0.0)")
  107. selectSpeedCallBack!(lastSelectModel?.maxSpeed ?? 0.0, lastSelectModel?.minSpeed ?? 0.0)
  108. }
  109. }
  110. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  111. // 20 是 cell label 上下边距总和
  112. if viewType == 1 {
  113. if indexPath.row == datas.count - 1 {
  114. return CGSize(width: 65, height: 44)
  115. }
  116. return CGSize(width: 44 + 10, height: 24 + 20)
  117. } else {
  118. if indexPath.row == 0 || indexPath.row == datas.count - 1 {
  119. return CGSize(width: 60 + 10, height: 30)
  120. }
  121. return CGSize(width: 30 + 10, height: 30)
  122. }
  123. }
  124. }
  125. class PQSpeedTitleModel: NSObject {
  126. // UI 上显示的文字
  127. var title: String = ""
  128. // 是否已经选择
  129. var isSelected: Bool = false
  130. // 最大、最小速度
  131. var maxSpeed: Float = 0.0
  132. var minSpeed: Float = 0.0
  133. public override init() {
  134. super.init()
  135. }
  136. }
  137. class PQSpeedTitleCell: UICollectionViewCell {
  138. lazy var titleLab: UILabel = {
  139. let titleLab = UILabel()
  140. titleLab.font = UIFont.systemFont(ofSize: 13, weight: .regular)
  141. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  142. titleLab.numberOfLines = 0
  143. titleLab.lineBreakMode = .byCharWrapping
  144. titleLab.isUserInteractionEnabled = true
  145. titleLab.textAlignment = .center
  146. titleLab.addCorner(corner: 5)
  147. return titleLab
  148. }()
  149. override init(frame: CGRect) {
  150. super.init(frame: frame)
  151. contentView.addSubview(titleLab)
  152. titleLab.snp.remakeConstraints { make in
  153. make.height.equalToSuperview()
  154. make.width.equalToSuperview().offset(-10)
  155. make.left.equalToSuperview()
  156. make.top.equalToSuperview()
  157. }
  158. }
  159. required init?(coder _: NSCoder) {
  160. fatalError("init(coder:) has not been implemented")
  161. }
  162. var titleModel: PQSpeedTitleModel? {
  163. didSet {
  164. titleLab.text = titleModel?.title
  165. titleLab.snp.remakeConstraints { make in
  166. make.height.equalToSuperview()
  167. make.width.equalToSuperview().offset(-10)
  168. make.left.equalToSuperview()
  169. make.top.equalToSuperview()
  170. }
  171. if titleModel?.isSelected ?? false {
  172. titleLab.backgroundColor = UIColor(red: 0.24, green: 0.758, blue: 0.758, alpha: 0.15)
  173. titleLab.textColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  174. } else {
  175. titleLab.backgroundColor = UIColor.hexColor(hexadecimal: "#F9F9F9")
  176. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  177. }
  178. }
  179. }
  180. }