91 lines
2.3 KiB
Swift
91 lines
2.3 KiB
Swift
//
|
|
// VPHomePageControlView.swift
|
|
// Veloria
|
|
//
|
|
// Created by 湖南秦九 on 2025/5/27.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class VPHomePageControlView: UIView {
|
|
|
|
var currentIndex: Int = 0 {
|
|
didSet {
|
|
if count == 0 { return }
|
|
if currentIndex >= count {
|
|
currentIndex = 0
|
|
return
|
|
}
|
|
|
|
for view in self.stackView.arrangedSubviews {
|
|
if let view = view as? UIButton {
|
|
view.isSelected = view.tag == currentIndex
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
var count = 0 {
|
|
didSet {
|
|
self.stackView.removeAllArrangedSubview()
|
|
|
|
for i in 0..<count {
|
|
let view = createDotView()
|
|
view.tag = i
|
|
view.isSelected = i == currentIndex
|
|
stackView.addArrangedSubview(view)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private lazy var stackView: UIStackView = {
|
|
let view = UIStackView()
|
|
view.axis = .horizontal
|
|
view.spacing = 4
|
|
return view
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
addSubview(stackView)
|
|
|
|
stackView.snp.makeConstraints { make in
|
|
make.edges.equalToSuperview()
|
|
}
|
|
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
|
|
private func createDotView() -> UIButton {
|
|
var config = UIButton.Configuration.plain()
|
|
config.contentInsets = .zero
|
|
|
|
let button = UIButton(configuration: config)
|
|
button.configurationUpdateHandler = { [weak self] button in
|
|
guard let _ = self else { return }
|
|
if button.isSelected {
|
|
button.backgroundColor = .color7C174F()
|
|
button.layer.borderWidth = 0
|
|
} else {
|
|
button.backgroundColor = .clear
|
|
button.layer.borderWidth = 1
|
|
}
|
|
}
|
|
button.layer.cornerRadius = 3
|
|
button.layer.masksToBounds = true
|
|
button.layer.borderColor = UIColor.colorFFFFFF(alpha: 0.35).cgColor
|
|
button.snp.makeConstraints { make in
|
|
make.width.height.equalTo(6)
|
|
}
|
|
|
|
return button
|
|
}
|
|
|
|
}
|