123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- //
- // TSAIChangeEmoteStyleView.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/4/10.
- //
- class TSAIChangeEmoteStyleView:TSBaseView {
-
- var selectedValueBlock:((TSGenerateStyleModel)->Void)?
-
- var dataArray: [TSGenerateStyleModel] = [TSGenerateStyleModel](){
- didSet{
- styleCollectionView.reloadData()
- if dataArray.count > 0 {
- self.styleCollectionView.selectItem(at: self.currentIndexPath, animated: false, scrollPosition: .centeredHorizontally)
- }
- }
- }
-
- lazy var layout: UICollectionViewFlowLayout = {
- let layout = UICollectionViewFlowLayout()
- layout.scrollDirection = .horizontal
- layout.itemSize = CGSize(width: 60, height: 100)
- layout.minimumInteritemSpacing = 12.0
- // layout.minimumLineSpacing = 12.0
- layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
- return layout
- }()
-
-
- lazy var styleCollectionView: UICollectionView = {
- let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
- collectionView.delegate = self
- collectionView.dataSource = self
- collectionView.showsVerticalScrollIndicator = false
- collectionView.showsHorizontalScrollIndicator = false
- collectionView.backgroundColor = .clear
- collectionView.register(TSAIChangeEmoteStyleCell.self, forCellWithReuseIdentifier: TSAIChangeEmoteStyleCell.cellID)
- if #available(iOS 11.0, *) {
- collectionView.contentInsetAdjustmentBehavior = .never
- }
- return collectionView
- }()
- lazy var willSelectIndexPath = currentIndexPath
- var currentIndexPath:IndexPath = IndexPath(item: 0, section: 0){
- didSet{
- for (i,model) in dataArray.enumerated(){
- if i == currentIndexPath.item {
- model.isSelected = true
- }else {
- model.isSelected = false
- }
- }
- styleCollectionView.reloadData()
- }
- }
-
- override func creatUI() {
- currentIndexPath = IndexPath(item: 0, section: 0)
- contentView.addSubview(styleCollectionView)
- styleCollectionView.snp.makeConstraints { make in
- make.top.equalTo(0)
- make.leading.trailing.equalTo(0)
- make.height.equalTo(0)
- make.bottom.equalTo(0)
- }
- }
-
- func unCheck(){
- styleCollectionView.deselectItem(at: currentIndexPath, animated: true)
- }
-
- func agreeWillSelectIndexPath(){
- currentIndexPath = willSelectIndexPath
- }
-
- }
- extension TSAIChangeEmoteStyleView: UICollectionViewDataSource ,UICollectionViewDelegate {
-
- public func numberOfSections(in collectionView: UICollectionView) -> Int {
- return 1
- }
-
- public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return dataArray.count
- }
-
- public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSAIChangeEmoteStyleCell.cellID, for: indexPath)
- if let cell = cell as? TSAIChangeEmoteStyleCell,let itemModel = dataArray.safeObj(At: indexPath.item){
- cell.itemModel = itemModel
- }
- return cell
- }
- public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- if let model = dataArray.safeObj(At: indexPath.item){
- willSelectIndexPath = indexPath
- selectedValueBlock?(model)
- }
- }
-
-
- // // 在 didSelectItemAt 方法中处理选中
- // func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- //
- // if let model = dataArray.safeObj(At: indexPath.item),let selectedValueBlock = selectedValueBlock{
- // if selectedValueBlock(model){
- // currentIndexPath = indexPath
- // handleSelectedCellForItem(at: indexPath, isSelected: true)
- // }else{
- // handleSelectedCellForItem(at: indexPath, isSelected: false)
- // }
- // }
- //
- //
- // }
- //
- //// func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
- //// if let cell = collectionView.cellForItem(at: indexPath) {
- //// cell.isSelected = false
- //// // 可以在这里更新 UI 以反映取消选中状态
- //// }
- //// }
- //
- // func handleSelectedCellForItem(at indexPath: IndexPath,isSelected:Bool){
- // if let cell = styleCollectionView.cellForItem(at: indexPath) {
- // cell.isSelected = true
- // }
- // }
- }
- class TSAIChangeEmoteStyleCell: TSBaseCollectionCell {
-
- static let cellID = "TSAIChangeEmoteStyleCell"
- // override var isSelected: Bool{
- // didSet{
- // boardImageView.isHidden = isSelected ? false : true
- // }
- // }
-
- var itemModel:TSGenerateStyleModel = TSGenerateStyleModel(){
- didSet{
- imageView.image = UIImage(named: itemModel.imageName)
- vipImageView.isHidden = itemModel.isVip == false
- textLabel.text = itemModel.imageText.localized
- boardImageView.isHidden = itemModel.isSelected ? false : true
- }
- }
-
- lazy var imageView: UIImageView = {
- let imageView = UIImageView()
- imageView.cornerRadius = 8.0
- return imageView
- }()
-
- lazy var vipImageView: UIImageView = {
- let vipImageView = UIImageView.createImageView(imageName: "ai_emo_style_vip")
- vipImageView.isHidden = true
- return vipImageView
- }()
-
- lazy var boardImageView: UIImageView = {
- let boardImageView = UIImageView.createImageView(imageName: "ai_emo_selected_border",contentMode: .scaleToFill)
- boardImageView.isHidden = true
- return boardImageView
- }()
-
- lazy var textLabel: UILabel = {
- let textLabel = UILabel.createLabel(font: .font(size: 10,weight: .medium),textColor: .white,textAlignment: .center,numberOfLines: 0)
- return textLabel
- }()
-
- override func creatUI() {
- //cell 100*110
-
- bgContentView.addSubview(imageView)
- imageView.snp.makeConstraints { make in
- make.top.equalTo(0)
- make.centerX.equalToSuperview()
- make.width.equalTo(60)
- make.height.equalTo(80)
- }
-
- imageView.addSubview(vipImageView)
- vipImageView.snp.makeConstraints { make in
- make.trailing.equalTo(0)
- make.top.equalTo(0.0)
- make.width.equalTo(18)
- make.height.equalTo(14)
- }
-
- imageView.addSubview(boardImageView)
- boardImageView.snp.makeConstraints { make in
- make.top.bottom.leading.trailing.equalTo(0)
- }
-
- bgContentView.addSubview(textLabel)
- textLabel.snp.makeConstraints { make in
- // make.top.equalTo(imageView.snp.bottom).offset(2)
- make.bottom.equalTo(0)
- make.height.equalTo(16.0)
- make.leading.trailing.equalToSuperview()
- }
- }
-
- }
|