PQSpeedSettingView.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. 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, 3, 2.4, 1.0, 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. var str:String = (viewType == 2) ? "跳跃" : "循环"
  63. let tempTitle =
  64. ["\(str)1x",
  65. "2x",
  66. "3x",
  67. "4x",
  68. "5x",
  69. "自定义"]
  70. let tempMaxSpeed = [1, 2, 3, 4, 5, 0]
  71. for (index, str) in tempTitle.enumerated() {
  72. let model = PQSpeedTitleModel()
  73. model.title = str
  74. model.maxSpeed = Float(tempMaxSpeed[index])
  75. datas.append(model)
  76. }
  77. }
  78. titleCollectionView.reloadData()
  79. }
  80. }
  81. // 点击回调 maxSpeed,minSpeed 同时为0 说明点击的是自定义速度
  82. public var selectSpeedCallBack: ((_ maxSpeed: Float, _ minSpeed: Float,_ selectIndex:Int) -> Void)?
  83. override init(frame: CGRect) {
  84. super.init(frame: frame)
  85. addSubview(titleCollectionView)
  86. }
  87. required init?(coder _: NSCoder) {
  88. fatalError("init(coder:) has not been implemented")
  89. }
  90. //设置默认选择的
  91. ///
  92. /// - Parameter index: 第几位 从0 开始
  93. func setSelectItem(index:Int) {
  94. BFLog(message: "setSelectItem is \(index)")
  95. lastSelectModel?.isSelected = false
  96. if(viewType == 4){
  97. if(index > 4){
  98. let model = PQSpeedTitleModel()
  99. model.title = "\(index + 1)x"
  100. model.maxSpeed = Float(index + 1)
  101. datas.insert(model, at: 5)
  102. lastSelectModel = datas[5]
  103. }
  104. }else{
  105. lastSelectModel = datas[index]
  106. }
  107. lastSelectModel?.isSelected = true
  108. titleCollectionView.reloadData()
  109. //发出回调,调用方走统一处理逻辑
  110. if selectSpeedCallBack != nil {
  111. BFLog(message: "选择的速度为 max: \(lastSelectModel?.maxSpeed ?? 0.0) min: \(lastSelectModel?.minSpeed ?? 0.0)")
  112. if(lastSelectModel?.title == "自定义"){
  113. selectSpeedCallBack!(-1,-1,index)
  114. }else{
  115. selectSpeedCallBack!(lastSelectModel?.maxSpeed ?? 0.0, lastSelectModel?.minSpeed ?? 0.0,index)
  116. }
  117. }
  118. }
  119. }
  120. extension PQSpeedSettingView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  121. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  122. return datas.count
  123. }
  124. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  125. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQSpeedTitleCell.self), for: indexPath) as! PQSpeedTitleCell
  126. cell.titleModel = datas[indexPath.row]
  127. return cell
  128. }
  129. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  130. BFLog(message: "选择了 \(String(describing: datas[indexPath.row]))")
  131. setSelectItem(index: indexPath.row)
  132. }
  133. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  134. // 20 是 cell label 上下边距总和
  135. if viewType == 1 {
  136. if indexPath.row == datas.count - 1 {
  137. return CGSize(width: 65, height: 44)
  138. }
  139. return CGSize(width: 44 + 10, height: 24 + 20)
  140. } else {
  141. if indexPath.row == 0 || indexPath.row == datas.count - 1 {
  142. return CGSize(width: 60 + 10, height: 30)
  143. }
  144. return CGSize(width: 30 + 10, height: 30)
  145. }
  146. }
  147. }
  148. class PQSpeedTitleModel: NSObject {
  149. // UI 上显示的文字
  150. var title: String = ""
  151. // 是否已经选择
  152. var isSelected: Bool = false
  153. // 最大、最小速度
  154. var maxSpeed: Float = 0.0
  155. var minSpeed: Float = 0.0
  156. public override init() {
  157. super.init()
  158. }
  159. }
  160. class PQSpeedTitleCell: UICollectionViewCell {
  161. lazy var titleLab: UILabel = {
  162. let titleLab = UILabel()
  163. titleLab.font = UIFont.systemFont(ofSize: 13, weight: .regular)
  164. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  165. titleLab.numberOfLines = 0
  166. titleLab.lineBreakMode = .byCharWrapping
  167. titleLab.isUserInteractionEnabled = true
  168. titleLab.textAlignment = .center
  169. titleLab.addCorner(corner: 5)
  170. return titleLab
  171. }()
  172. override init(frame: CGRect) {
  173. super.init(frame: frame)
  174. contentView.addSubview(titleLab)
  175. titleLab.snp.remakeConstraints { make in
  176. make.height.equalToSuperview()
  177. make.width.equalToSuperview().offset(-10)
  178. make.left.equalToSuperview()
  179. make.top.equalToSuperview()
  180. }
  181. }
  182. required init?(coder _: NSCoder) {
  183. fatalError("init(coder:) has not been implemented")
  184. }
  185. var titleModel: PQSpeedTitleModel? {
  186. didSet {
  187. titleLab.text = titleModel?.title
  188. titleLab.snp.remakeConstraints { make in
  189. make.height.equalToSuperview()
  190. make.width.equalToSuperview().offset(-10)
  191. make.left.equalToSuperview()
  192. make.top.equalToSuperview()
  193. }
  194. if titleModel?.isSelected ?? false {
  195. titleLab.backgroundColor = UIColor(red: 0.24, green: 0.758, blue: 0.758, alpha: 0.15)
  196. titleLab.textColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  197. } else {
  198. titleLab.backgroundColor = UIColor.hexColor(hexadecimal: "#F9F9F9")
  199. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  200. }
  201. }
  202. }
  203. }