jsdw_ios/QuickLocation/Section/Group/CreateGroup/GroupIconListView.swift

243 lines
6.9 KiB
Swift

//
// GroupIconListView.swift
// QuickLocation
//
// Created by on 2026/6/2.
//
import UIKit
import RxSwift
import RxCocoa
class GroupIconListView: UIView {
var disposeBag = DisposeBag()
var selectedIndex: Int = 1 {
didSet {
selectedIconView.image = UIImage(named: "GroupIcon/\(selectedIndex)")
iconCollectionView.reloadData()
}
}
private var iconImgList: [UIImage] = []
private func setupRx() {
backBtn.rx.tap.subscribe(onNext: { _ in
AppRouter.shared.popOrDismiss()
}).disposed(by: disposeBag)
}
private func setupUI() {
addSubview(navBgView)
addSubview(navBarView)
navBarView.addSubview(navTitleLabel)
navBarView.addSubview(backBtn)
navBarView.addSubview(doneBtn)
addSubview(selectedIconView)
addSubview(titleLab)
addSubview(iconCollectionView)
navBgView.layoutChain
.edges(excludingEdge: .bottom)
.heightToWidth(160/375)
navBarView.layoutChain
.edges(excludingEdge: .bottom)
.height(kNaviHeight)
navTitleLabel.layoutChain
.top(kStatusBarHeight + 12)
.centerY(backBtn)
.centerX()
backBtn.layoutChain
.centerY(navTitleLabel)
.left(15)
.width(24)
.height(24)
doneBtn.layoutChain
.centerY(navTitleLabel)
.right(15)
selectedIconView.layoutChain
.topToBottomOfView(navBarView, offset: 30)
.centerX()
.width(80)
.height(80)
titleLab.layoutChain
.topToBottomOfView(selectedIconView, offset: 39)
.left(40)
iconCollectionView.layoutChain
.topToBottomOfView(titleLab, offset: 13)
.edgesHorzontal(40)
.bottom(kSafeBottomMargin + 10)
doneBtn.sizeToFit()
}
lazy var navBgView: UIImageView = {
let iv = UIImageView()
iv.image = UIImage(named: "Common/navBar_bg_2")
iv.contentMode = .scaleAspectFill
return iv
}()
lazy var navBarView: UIView = {
let view = UIView()
view.backgroundColor = .clear
return view
}()
lazy var navTitleLabel: UILabel = {
let label = UILabel()
label.text = "更换图标"
label.font = .systemFont(ofSize: 18, weight: .medium)
label.textColor = ThemeManager.shared.color.titleAuxColor
label.textAlignment = .center
return label
}()
lazy var backBtn: UIButton = {
let btn = UIButton(type: .custom)
btn.setImage(UIImage(named: "Common/back"), for: .normal)
btn.extendEdgeInsets = UIEdgeInsets(top: 54, left: 15, bottom: 100, right: 100)
return btn
}()
lazy var doneBtn: UIButton = {
let btn = UIButton(type: .custom)
btn.setTitle("完成", for: .normal)
btn.setTitleColor(ThemeManager.shared.color.titleAuxColor, for: .normal)
btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
btn.extendEdgeInsets = UIEdgeInsets(top: 54, left: 100, bottom: 100, right: 15)
return btn
}()
lazy var selectedIconView: UIImageView = {
let view = UIImageView()
view.cornerRadius = 40
return view
}()
lazy var titleLab: UILabel = {
let label = UILabel()
label.text = "选择图标"
label.font = .systemFont(ofSize: 16, weight: .bold)
label.textColor = ThemeManager.shared.color.titleAuxColor
return label
}()
lazy var iconCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cvWidth = kScreenWidth - 80
let spacing: CGFloat = 18
let itemW = (cvWidth - spacing * 3) / 4
layout.itemSize = CGSize(width: itemW, height: itemW)
layout.minimumInteritemSpacing = spacing
layout.minimumLineSpacing = 20
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .clear
cv.isScrollEnabled = false
cv.register(GroupIconCell.self)
cv.delegate = self
cv.dataSource = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: .zero)
backgroundColor = .white
setupUI()
setupRx()
for i in 1...11 {
if let img = UIImage(named: "GroupIcon/\(i)") {
iconImgList.append(img)
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: - UICollectionViewDelegate, UICollectionViewDataSource
extension GroupIconListView: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return iconImgList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(for: indexPath) as GroupIconCell
cell.configure(img: iconImgList[indexPath.row], isSelected: selectedIndex == indexPath.row+1)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndex = indexPath.row + 1
}
}
// MARK: - TagCell
final class GroupIconCell: UICollectionViewCell {
func configure(img: UIImage, isSelected: Bool) {
iconImgView.image = img
selectedMaskView.isHidden = !isSelected
selectedIcon.isHidden = !isSelected
}
private func setupUI() {
contentView.addSubview(iconImgView)
contentView.addSubview(selectedMaskView)
contentView.addSubview(selectedIcon)
iconImgView.layoutChain.edges()
selectedMaskView.layoutChain.edges()
selectedIcon.layoutChain
.right(3)
.bottom(4)
}
lazy var iconImgView: UIImageView = {
let view = UIImageView()
view.backgroundColor = .clear
view.contentMode = .scaleAspectFill
view.cornerRadius = 10
return view
}()
lazy var selectedMaskView: UIView = {
let view = UIView()
view.backgroundColor = .black.withAlphaComponent(0.5)
view.cornerRadius = 10
view.isHidden = true
return view
}()
lazy var selectedIcon: UIImageView = {
let view = UIImageView()
view.image = UIImage(named: "GroupIcon/selected")
view.isHidden = true
return view
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}