Преглед изворни кода

自定义速度 和固定速度 UI

jsonwang пре 3 година
родитељ
комит
d0d32377cb

+ 131 - 0
BFFramework/Classes/Stuckpoint/View/PQCustomSpeedSettingView.swift

@@ -0,0 +1,131 @@
+//
+//  PQCustomSpeedSettingView.swift
+//  BFFramework
+//
+//  Created by ak on 2021/8/3.
+//  功能:自定义速度界面
+
+import Foundation
+
+class PQCustomSpeedSettingView: UIView {
+    
+    //左上角返回
+    lazy var backBtn: UIButton = {
+        let backBtn = UIButton(type: .custom)
+        backBtn.addTarget(self, action: #selector(backClick(sender:)), for: .touchUpInside)
+        backBtn.setImage(UIImage(named: "customSpeedClosed"), for: .normal)
+        backBtn.adjustsImageWhenHighlighted = false
+        backBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#3DC1C1")
+        backBtn.addCorner(corner: 30)
+        return backBtn
+    }()
+    
+    //标题
+    public lazy var titleLab: UILabel = {
+        let titleLab = UILabel()
+        titleLab.font = UIFont.systemFont(ofSize: 14, weight: .medium)
+        titleLab.textAlignment = .center
+        titleLab.text = "自定义快慢速"
+        titleLab.textColor =  UIColor.hexColor(hexadecimal: "#353535")
+        return titleLab
+    }()
+
+    //确定
+    public lazy var confirmBtn: UIButton = {
+        let confirmBtn = UIButton(type: .custom)
+        confirmBtn.setTitle("确定", for: .normal)
+        confirmBtn.setTitleColor(.white, for: .normal)
+        confirmBtn.addTarget(self, action: #selector(confirmClick(sender:)), for: .touchUpInside)
+        confirmBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#3DC1C1")
+        confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
+        return confirmBtn
+    }()
+ 
+    //取消
+    public lazy var cancleBtn: UIButton = {
+        let cancleBtn = UIButton(type: .custom)
+        cancleBtn.setTitle("取消", for: .normal)
+        cancleBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#3DC1C1"), for: .normal)
+        cancleBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#F9F9F9")
+        cancleBtn.addTarget(self, action: #selector(backClick(sender:)), for: .touchUpInside)
+        cancleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .medium)
+        return cancleBtn
+    }()
+    
+    //提示1
+    lazy var customSpeedFastView: UIImageView = {
+        let customSpeedFastView = UIImageView(image: UIImage(named: "customSpeedFast"))
+        return customSpeedFastView
+    }()
+    //提示2
+    lazy var customSpeedSlowView: UIImageView = {
+        let customSpeedSlowView = UIImageView(image: UIImage(named: "customSpeedSlow"))
+        return customSpeedSlowView
+    }()
+
+
+    
+    override init(frame: CGRect) {
+        super.init(frame: frame)
+        
+        addSubview(backBtn)
+        
+        autolayout()
+  
+    }
+    required init?(coder _: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+    
+    func autolayout() {
+        
+        backBtn.snp.makeConstraints { make in
+            make.height.width.equalTo(24)
+            make.left.equalToSuperview().offset(16)
+            make.top.equalToSuperview().offset(10)
+        }
+        
+        titleLab.snp.makeConstraints { make in
+            make.height.equalTo(20)
+            make.width.equalTo(150)
+            make.left.equalToSuperview().offset(16)
+            make.top.equalToSuperview().offset(10)
+        }
+        customSpeedFastView.snp.makeConstraints { make in
+            make.height.width.equalTo(30)
+            make.left.equalToSuperview().offset(18)
+            make.top.equalToSuperview().offset(98)
+        }
+        
+        customSpeedSlowView.snp.makeConstraints { make in
+            make.height.width.equalTo(30)
+            make.left.equalTo(customSpeedFastView.snp_left)
+            make.top.equalTo(customSpeedFastView.snp_bottom).offset(31)
+        }
+        
+        customSpeedFastView.snp.makeConstraints { make in
+            make.height.equalTo(50)
+            make.width.equalTo(61)
+            make.left.equalToSuperview().offset(16)
+            make.bottom.equalToSuperview().offset(60)
+        }
+        
+        customSpeedSlowView.snp.makeConstraints { make in
+            make.height.equalTo(50)
+            make.width.equalTo(254)
+            make.right.equalToSuperview().offset(-30)
+            make.top.equalTo(customSpeedFastView.snp_top)
+        }
+ 
+    }
+    
+    //返回
+    @objc func backClick(sender: UIButton){
+        isHidden = true
+    }
+    
+    //确认
+    @objc  func confirmClick(sender: UIButton) {
+    
+    }
+}

+ 211 - 0
BFFramework/Classes/Stuckpoint/View/PQSpeedSettingView.swift

@@ -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")
+            }
+        }
+    }
+}