//
|
// RandromAnsterView.swift
|
// DolphinEnglishLearnStudent
|
//
|
// Created by 无故事王国 on 2025/9/2.
|
//
|
|
import UIKit
|
let CellW = JQ_ScreenW * 0.0336
|
|
class RandromAnsterView: UIView {
|
private var count:Int = 0
|
private var selectIndex:IndexPath?
|
private var clickClouse:((Int)->Void)?
|
|
private var collectionView:UICollectionView = {
|
let uICollectionViewFlowLayout = UICollectionViewFlowLayout()
|
uICollectionViewFlowLayout.itemSize = CGSizeMake(CellW, CellW)
|
uICollectionViewFlowLayout.minimumLineSpacing = 20
|
uICollectionViewFlowLayout.minimumInteritemSpacing = 20
|
let collectionV = UICollectionView(frame: .zero, collectionViewLayout: uICollectionViewFlowLayout)
|
collectionV.register(RandromAnsterCCell.self, forCellWithReuseIdentifier: "_RandromAnsterCCell")
|
return collectionV
|
}()
|
|
private var contentView:UIView = {
|
let v = UIView()
|
v.backgroundColor = .white
|
v.jq_cornerRadius = 10
|
return v
|
}()
|
|
private var closeBtn:UIButton = {
|
let btn = UIButton(type: .custom)
|
btn.setTitle("关闭", for: .normal)
|
btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
|
btn.jq_borderWidth = 1
|
btn.layer.cornerRadius = 4
|
btn.layer.borderColor = UIColor(hexString:"#41A2EB")?.cgColor
|
btn.setTitleColor(UIColor(hexString:"#41A2EB"), for: .normal)
|
return btn
|
}()
|
|
private var commitBtn:UIButton = {
|
let btn = UIButton(type: .custom)
|
btn.setTitle("确认", for: .normal)
|
btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
|
btn.jq_borderWidth = 1
|
btn.layer.cornerRadius = 4
|
btn.layer.borderColor = UIColor(hexString:"#41A2EB")?.cgColor
|
btn.setTitleColor(UIColor(hexString:"#41A2EB"), for: .normal)
|
return btn
|
}()
|
|
override init(frame: CGRect) {
|
super.init(frame: frame)
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
closeBtn.addTarget(self, action: #selector(closeAction), for: .touchUpInside)
|
commitBtn.addTarget(self, action: #selector(commitAction), for: .touchUpInside)
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
static func show(_ count:Int = 0,clickClouse:@escaping (Int)->Void){
|
let randromAnsterView = RandromAnsterView(frame: .zero)
|
randromAnsterView.clickClouse = clickClouse
|
randromAnsterView.count = count
|
randromAnsterView.frame = sceneDelegate?.window?.frame ?? .zero
|
randromAnsterView.backgroundColor = UIColor(hexString: "#484849")
|
randromAnsterView.contentView.transform = .init(scaleX: 0.1, y: 0.1)
|
randromAnsterView.alpha = 0
|
sceneDelegate?.window?.addSubview(randromAnsterView)
|
|
randromAnsterView.addSubview(randromAnsterView.contentView)
|
randromAnsterView.contentView.snp.makeConstraints { make in
|
make.center.equalToSuperview()
|
make.width.equalTo(JQ_ScreenW * 0.3433)
|
make.height.equalTo(JQ_ScreenW * 0.3433 * 0.7)
|
}
|
|
randromAnsterView.closeBtn.snp.makeConstraints { make in
|
make.width.equalTo(124)
|
make.height.equalTo(40)
|
}
|
|
randromAnsterView.commitBtn.snp.makeConstraints { make in
|
make.width.equalTo(124)
|
make.height.equalTo(40)
|
}
|
|
let uIStackView = UIStackView(arrangedSubviews: [randromAnsterView.closeBtn,randromAnsterView.commitBtn])
|
uIStackView.axis = .horizontal
|
uIStackView.spacing = 27
|
uIStackView.distribution = .equalSpacing
|
randromAnsterView.contentView.addSubview(uIStackView)
|
uIStackView.snp.makeConstraints { make in
|
make.bottom.equalToSuperview().offset(-20)
|
make.width.equalTo(275)
|
make.centerX.equalToSuperview()
|
}
|
|
let titleL = UILabel()
|
titleL.text = "标题"
|
titleL.font = .systemFont(ofSize: 18, weight: .medium)
|
titleL.textColor = UIColor.black.withAlphaComponent(0.8)
|
randromAnsterView.contentView.addSubview(titleL)
|
titleL.snp.makeConstraints { make in
|
make.top.equalToSuperview().offset(32)
|
make.centerX.equalToSuperview()
|
make.height.equalTo(18)
|
}
|
|
randromAnsterView.contentView.addSubview(randromAnsterView.collectionView)
|
randromAnsterView.collectionView.snp.makeConstraints { make in
|
make.top.equalTo(titleL.snp.bottom).offset(35)
|
make.leading.equalTo(34)
|
make.trailing.equalTo(-34)
|
make.bottom.equalTo(uIStackView.snp.top).offset(-5)
|
}
|
|
UIView.animate(withDuration: 0.2) {
|
randromAnsterView.contentView.transform = .identity
|
randromAnsterView.alpha = 1
|
} completion: { _ in
|
|
}
|
}
|
|
@objc func closeAction(){
|
UIView.animate(withDuration: 0.2) {
|
self.contentView.transform = .init(scaleX: 0.1, y: 0.1)
|
self.alpha = 0
|
} completion: { _ in
|
self.removeFromSuperview()
|
}
|
}
|
|
@objc func commitAction(){
|
UIView.animate(withDuration: 0.2) {
|
self.contentView.transform = .init(scaleX: 0.1, y: 0.1)
|
self.alpha = 0
|
} completion: { _ in
|
if let index = self.selectIndex{
|
self.clickClouse?(index.row)
|
}
|
self.removeFromSuperview()
|
}
|
}
|
}
|
|
extension RandromAnsterView:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
// clickClouse?(indexPath.row)
|
selectIndex = indexPath
|
collectionView.reloadData()
|
}
|
}
|
|
extension RandromAnsterView:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return count
|
}
|
|
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
return 1
|
}
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_RandromAnsterCCell", for: indexPath) as! RandromAnsterCCell
|
cell.titleL.text = "\(indexPath.row + 1)"
|
|
if indexPath.row == selectIndex?.row{
|
cell.titleL.jq_borderColor = UIColor(hexString: "#41A2EB")
|
cell.titleL.textColor = UIColor(hexString: "#41A2EB")
|
}else{
|
cell.titleL.jq_borderColor = .black.withAlphaComponent(0.2)
|
cell.titleL.textColor = .black.withAlphaComponent(0.6)
|
}
|
return cell
|
}
|
}
|
|
class RandromAnsterCCell:UICollectionViewCell{
|
|
|
private(set) var titleL:UILabel!
|
|
override init(frame: CGRect) {
|
super.init(frame: frame)
|
titleL = UILabel()
|
contentView.addSubview(titleL)
|
titleL.textAlignment = .center
|
titleL.font = UIFont.systemFont(ofSize: 18, weight: .medium)
|
titleL.jq_borderWidth = 1
|
titleL.textColor = .black.withAlphaComponent(0.6)
|
titleL.snp.makeConstraints { make in
|
make.edges.equalToSuperview()
|
}
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
}
|