151 lines
4.5 KiB
Swift
151 lines
4.5 KiB
Swift
//
|
|
// SPEpisodeMenuView.swift
|
|
// Thimra
|
|
//
|
|
// Created by 曾觉新 on 2025/4/17.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class SPEpisodeMenuView: UIView {
|
|
|
|
override var intrinsicContentSize: CGSize {
|
|
return CGSize(width: kSPScreenWidth, height: 30)
|
|
}
|
|
|
|
var didSelectedIndex: ((_ index: Int) -> Void)?
|
|
|
|
var dataArr: [String] = [] {
|
|
didSet {
|
|
self.reloadData()
|
|
}
|
|
}
|
|
|
|
var selectedIndex: Int = 0 {
|
|
didSet {
|
|
self.buttonArr.forEach {
|
|
$0.isSelected = $0.tag == selectedIndex
|
|
}
|
|
self.progressSlide()
|
|
}
|
|
}
|
|
|
|
|
|
private lazy var buttonArr: [UIButton] = []
|
|
|
|
//MARK: UI属性
|
|
private lazy var progressView: UIView = {
|
|
let view = UIView()
|
|
view.backgroundColor = .colorFF0000()
|
|
// view.colors = [UIColor.colorF56490().cgColor, UIColor.colorBF6BFF().cgColor]
|
|
// view.startPoint = .init(x: 0, y: 0.5)
|
|
// view.endPoint = .init(x: 1, y: 0.5)
|
|
// view.locations = [0, 1]
|
|
return view
|
|
}()
|
|
|
|
private lazy var scrollView: SPScrollView = {
|
|
let scrollView = SPScrollView()
|
|
scrollView.showsVerticalScrollIndicator = false
|
|
scrollView.showsHorizontalScrollIndicator = false
|
|
return scrollView
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
_setupUI()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
private func reloadData() {
|
|
buttonArr.forEach {
|
|
$0.removeFromSuperview()
|
|
}
|
|
buttonArr.removeAll()
|
|
|
|
let count = self.dataArr.count
|
|
|
|
var previousButton: UIButton?
|
|
|
|
dataArr.enumerated().forEach {
|
|
let normalStrig = NSMutableAttributedString(string: $1)
|
|
normalStrig.color = .colorFFFFFF()
|
|
normalStrig.font = .fontMedium(ofSize: 12)
|
|
|
|
// let selectedString = NSMutableAttributedString(string: $1)
|
|
// selectedString.color = .colorF564B6()
|
|
// selectedString.font = .fontMedium(ofSize: 14)
|
|
|
|
|
|
let button = UIButton(type: .custom)
|
|
button.tag = $0
|
|
button.setAttributedTitle(normalStrig, for: .normal)
|
|
// button.setAttributedTitle(selectedString, for: .selected)
|
|
// button.setAttributedTitle(selectedString, for: [.selected, .highlighted])
|
|
button.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
|
|
button.isSelected = $0 == selectedIndex
|
|
|
|
self.scrollView.addSubview(button)
|
|
self.buttonArr.append(button)
|
|
|
|
if previousButton == nil {
|
|
button.snp.makeConstraints { make in
|
|
make.top.left.equalToSuperview()
|
|
make.height.equalTo(30)
|
|
}
|
|
} else if let previousButton = previousButton, count - 1 == $0 {
|
|
button.snp.makeConstraints { make in
|
|
make.top.equalToSuperview()
|
|
make.left.equalTo(previousButton.snp.right).offset(25)
|
|
make.height.equalTo(30)
|
|
make.right.equalToSuperview()
|
|
}
|
|
} else if let previousButton = previousButton {
|
|
button.snp.makeConstraints { make in
|
|
make.top.equalToSuperview()
|
|
make.left.equalTo(previousButton.snp.right).offset(25)
|
|
make.height.equalTo(30)
|
|
}
|
|
}
|
|
|
|
previousButton = button
|
|
}
|
|
|
|
progressSlide()
|
|
}
|
|
|
|
private func progressSlide() {
|
|
if self.selectedIndex >= self.buttonArr.count { return }
|
|
let currentButton = self.buttonArr[self.selectedIndex]
|
|
|
|
self.progressView.snp.remakeConstraints { make in
|
|
make.bottom.width.equalTo(currentButton)
|
|
make.centerX.equalTo(currentButton)
|
|
make.height.equalTo(2)
|
|
}
|
|
}
|
|
|
|
|
|
@objc private func handleButton(sender: UIButton) {
|
|
self.selectedIndex = sender.tag
|
|
self.didSelectedIndex?(self.selectedIndex)
|
|
}
|
|
}
|
|
|
|
extension SPEpisodeMenuView {
|
|
|
|
private func _setupUI() {
|
|
addSubview(scrollView)
|
|
scrollView.addSubview(progressView)
|
|
|
|
scrollView.snp.makeConstraints { make in
|
|
make.left.right.top.equalToSuperview()
|
|
make.bottom.equalToSuperview()
|
|
}
|
|
}
|
|
|
|
}
|