|
@@ -0,0 +1,211 @@
|
|
|
+//
|
|
|
+// PQSpeedSettingView.swift
|
|
|
+// BFFramework
|
|
|
+//
|
|
|
+// Created by ak on 2021/8/2.
|
|
|
+// 功能:设置快慢速 跳越卡点 的倍速 VIEW
|
|
|
+
|
|
|
+import Foundation
|
|
|
+class PQSpeedSettingView: UIView {
|
|
|
+ // 速度列表
|
|
|
+ lazy var titleCollectionView: UICollectionView = {
|
|
|
+ let flowLayout = UICollectionViewFlowLayout()
|
|
|
+ flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
|
|
|
+ flowLayout.minimumLineSpacing = 0
|
|
|
+ flowLayout.minimumInteritemSpacing = 0
|
|
|
+ flowLayout.scrollDirection = .horizontal
|
|
|
+
|
|
|
+ let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
|
|
|
+
|
|
|
+ collectionView.showsVerticalScrollIndicator = false
|
|
|
+ collectionView.showsHorizontalScrollIndicator = false
|
|
|
+ collectionView.delegate = self
|
|
|
+ collectionView.dataSource = self
|
|
|
+ collectionView.backgroundColor = .clear
|
|
|
+ collectionView.register(PQSpeedTitleCell.self, forCellWithReuseIdentifier: String(describing: PQSpeedTitleCell.self))
|
|
|
+ if #available(iOS 11.0, *) {
|
|
|
+ collectionView.contentInsetAdjustmentBehavior = .never
|
|
|
+ }
|
|
|
+ // 延迟scrollView上子视图的响应,所以当直接拖动UISlider时,如果此时touch时间在150ms以内,UIScrollView会认为是拖动自己,从而拦截了event,导致UISlider接收不到滑动的event
|
|
|
+ collectionView.delaysContentTouches = false
|
|
|
+ return collectionView
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 保存数据
|
|
|
+ var datas: Array<PQSpeedTitleModel> = Array()
|
|
|
+ var lastSelectModel: PQSpeedTitleModel?
|
|
|
+ // view 初化的类型 1, 快慢速度卡点 2,跳跃卡点
|
|
|
+ var viewType: Int = 0 {
|
|
|
+ didSet {
|
|
|
+ titleCollectionView.snp.remakeConstraints { make in
|
|
|
+ make.right.equalToSuperview()
|
|
|
+ make.width.equalToSuperview()
|
|
|
+ make.height.equalTo(viewType == 1 ? 44 : 30)
|
|
|
+ make.top.equalToSuperview()
|
|
|
+ }
|
|
|
+ datas.removeAll()
|
|
|
+ if viewType == 1 {
|
|
|
+ let tempTitle =
|
|
|
+ ["6.0x\n1.2x",
|
|
|
+ "5.0x\n1.0x",
|
|
|
+ "3.0x\n0.5x",
|
|
|
+ "2.4x\n0.4x",
|
|
|
+ "1.0x\n0.3x",
|
|
|
+ "1.0x\n0.2x",
|
|
|
+ "自定义\n快慢速"]
|
|
|
+ let tempMaxSpeed = [6, 5, 4, 3, 2.4, 1.0, 1.0, 0.0]
|
|
|
+ let tempMinSpeed = [1.2, 1.0, 0.5, 0.4, 0.3, 0.2, 0.0]
|
|
|
+ for (index, str) in tempTitle.enumerated() {
|
|
|
+ let model = PQSpeedTitleModel()
|
|
|
+ model.title = str
|
|
|
+ model.maxSpeed = Float(tempMaxSpeed[index])
|
|
|
+ model.minSpeed = Float(tempMinSpeed[index])
|
|
|
+ datas.append(model)
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ let tempTitle =
|
|
|
+ ["跳跃1x",
|
|
|
+ "2x",
|
|
|
+ "3x",
|
|
|
+ "4x",
|
|
|
+ "5x",
|
|
|
+ "自定义"]
|
|
|
+ let tempMaxSpeed = [1, 2, 3, 4, 5, 0]
|
|
|
+ for (index, str) in tempTitle.enumerated() {
|
|
|
+ let model = PQSpeedTitleModel()
|
|
|
+ model.title = str
|
|
|
+ model.maxSpeed = Float(tempMaxSpeed[index])
|
|
|
+ datas.append(model)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ titleCollectionView.reloadData()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 点击回调 maxSpeed,minSpeed 同时为0 说明点击的是自定义速度
|
|
|
+ public var selectSpeedCallBack: ((_ maxSpeed: Float, _ minSpeed: Float) -> Void)?
|
|
|
+
|
|
|
+ override init(frame: CGRect) {
|
|
|
+ super.init(frame: frame)
|
|
|
+ addSubview(titleCollectionView)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ required init?(coder _: NSCoder) {
|
|
|
+ fatalError("init(coder:) has not been implemented")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+extension PQSpeedSettingView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
|
|
|
+ func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
|
|
|
+ return datas.count
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
|
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQSpeedTitleCell.self), for: indexPath) as! PQSpeedTitleCell
|
|
|
+
|
|
|
+ cell.titleModel = datas[indexPath.row]
|
|
|
+ return cell
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
|
+ BFLog(message: "选择了 \(String(describing: datas[indexPath.row]))")
|
|
|
+ lastSelectModel?.isSelected = false
|
|
|
+ datas[indexPath.row].isSelected = true
|
|
|
+ titleCollectionView.reloadData()
|
|
|
+
|
|
|
+ lastSelectModel = datas[indexPath.row]
|
|
|
+
|
|
|
+ if selectSpeedCallBack != nil {
|
|
|
+ BFLog(message: "选择的速度为 max: \(lastSelectModel?.maxSpeed ?? 0.0) min: lastSelectModel?.minSpeed ?? 0.0")
|
|
|
+ selectSpeedCallBack!(lastSelectModel?.maxSpeed ?? 0.0, lastSelectModel?.minSpeed ?? 0.0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
|
|
+ // 20 是 cell label 上下边距总和
|
|
|
+ if viewType == 1 {
|
|
|
+ if indexPath.row == datas.count - 1 {
|
|
|
+ return CGSize(width: 65, height: 44)
|
|
|
+ }
|
|
|
+ return CGSize(width: 44 + 10, height: 24 + 20)
|
|
|
+ } else {
|
|
|
+ if indexPath.row == 0 || indexPath.row == datas.count - 1 {
|
|
|
+ return CGSize(width: 60 + 10, height: 30)
|
|
|
+ }
|
|
|
+ return CGSize(width: 30 + 10, height: 30)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class PQSpeedTitleModel: NSObject {
|
|
|
+ // UI 上显示的文字
|
|
|
+ var title: String = ""
|
|
|
+ // 是否已经选择
|
|
|
+ var isSelected: Bool = false
|
|
|
+ // 对应的值
|
|
|
+ var titleValue = ""
|
|
|
+
|
|
|
+ // 最大、最小速度
|
|
|
+ var maxSpeed: Float = 0.0
|
|
|
+ var minSpeed: Float = 0.0
|
|
|
+ public override init() {
|
|
|
+ super.init()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class PQSpeedTitleCell: UICollectionViewCell {
|
|
|
+ lazy var titleLab: UILabel = {
|
|
|
+ let titleLab = UILabel()
|
|
|
+ titleLab.font = UIFont.systemFont(ofSize: 13, weight: .regular)
|
|
|
+ titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
|
|
|
+ titleLab.numberOfLines = 0
|
|
|
+ titleLab.lineBreakMode = .byCharWrapping
|
|
|
+ titleLab.isUserInteractionEnabled = true
|
|
|
+ titleLab.textAlignment = .center
|
|
|
+ titleLab.addCorner(corner: 5)
|
|
|
+
|
|
|
+ return titleLab
|
|
|
+ }()
|
|
|
+
|
|
|
+ override init(frame: CGRect) {
|
|
|
+ super.init(frame: frame)
|
|
|
+ contentView.addSubview(titleLab)
|
|
|
+ titleLab.snp.remakeConstraints { make in
|
|
|
+ make.height.equalToSuperview()
|
|
|
+ make.width.equalToSuperview().offset(-10)
|
|
|
+ make.left.equalToSuperview()
|
|
|
+ make.top.equalToSuperview()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ required init?(coder _: NSCoder) {
|
|
|
+ fatalError("init(coder:) has not been implemented")
|
|
|
+ }
|
|
|
+
|
|
|
+ var titleModel: PQSpeedTitleModel? {
|
|
|
+ didSet {
|
|
|
+ titleLab.text = titleModel?.title
|
|
|
+
|
|
|
+ titleLab.snp.remakeConstraints { make in
|
|
|
+ make.height.equalToSuperview()
|
|
|
+ make.width.equalToSuperview().offset(-10)
|
|
|
+ make.left.equalToSuperview()
|
|
|
+ make.top.equalToSuperview()
|
|
|
+ }
|
|
|
+
|
|
|
+ if titleModel?.isSelected ?? false {
|
|
|
+ titleLab.backgroundColor = UIColor(red: 0.24, green: 0.758, blue: 0.758, alpha: 0.15)
|
|
|
+ titleLab.textColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
|
|
|
+
|
|
|
+ } else {
|
|
|
+ titleLab.backgroundColor = UIColor.hexColor(hexadecimal: "#F9F9F9")
|
|
|
+ titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|