123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- //
- // TSDiscoverStyleMoreCell.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/6/23.
- //
- class TSDiscoverStyleMoreCell : TSDiscoverBaseCell {
- lazy var collectionView: UICollectionView = {
- let layout = UICollectionViewFlowLayout()
- layout.scrollDirection = .horizontal
- layout.minimumLineSpacing = 16
- layout.minimumInteritemSpacing = 16
- layout.itemSize = CGSize(width: 150, height: 200)
- let collectionView = TSBaseCollectionView(frame: .zero, collectionViewLayout: layout)
- collectionView.delegate = self
- collectionView.dataSource = self
- collectionView.register(TSDiscoverStyleMoreBaseCell.self, forCellWithReuseIdentifier: "TSDiscoverStyleMoreBaseCell")
- collectionView.register(TSDiscoverStyleMoreAnimationCellCell.self, forCellWithReuseIdentifier: "TSDiscoverStyleMoreAnimationCellCell")
- collectionView.register(TSDiscoverStyleMoreAnimationGifCell.self, forCellWithReuseIdentifier: "TSDiscoverStyleMoreAnimationGifCell")
- collectionView.contentInset = kDiscoverSection
- return collectionView
- }()
- var models:[TSDiscoverItemModel] = []
- override func upDateModel() {
- if let models = items as? [TSDiscoverItemModel] {
- self.models = models
- }
- collectionView.reloadData()
- }
-
- override func creatUI() {
- bgContentView.addSubview(collectionView)
- collectionView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- }
- }
- extension TSDiscoverStyleMoreCell: UICollectionViewDataSource ,UICollectionViewDelegate {
- public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return models.count
- }
-
- public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- if let dbModel = models.safeObj(At: indexPath.row) {
- if let animationModel = dbModel.viewModel as? TSDiscoverAnimationItemVM {
-
- if animationModel.style == .comparison {
- if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSDiscoverStyleMoreAnimationCellCell", for: indexPath) as? TSDiscoverStyleMoreAnimationCellCell{
- cell.model = dbModel
- return cell
- }
- }else if animationModel.style == .gif {
- if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSDiscoverStyleMoreAnimationGifCell", for: indexPath) as? TSDiscoverStyleMoreAnimationGifCell{
- cell.model = dbModel
- return cell
- }
- }
- }else{
- if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSDiscoverStyleMoreBaseCell", for: indexPath) as? TSDiscoverStyleMoreBaseCell{
- cell.model = dbModel
- return cell
- }
- }
- }
- return UICollectionViewCell()
- }
- public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- if let model = models.safeObj(At: indexPath.row) {
- clickBlock?(model)
- }
- }
- }
- private let CellCornerRadius = 16.0
- class TSDiscoverStyleMoreBaseCell: TSBaseCollectionCell {
- var model:TSDiscoverItemModel? {
- didSet {
- guard let model = model else { return }
- imageView.image = UIImage(named: model.viewModel.imageNamed)
- textLabel.text = model.name
- }
- }
- lazy var imageView: UIImageView = {
- let imageView = UIImageView.createImageView(contentMode: .scaleToFill,backgroundColor: .white.withAlphaComponent(0.2))
- imageView.cornerRadius = CellCornerRadius
- return imageView
- }()
-
-
- lazy var shadowView: UIImageView = {
- let imageView = UIImageView.createImageView(image:.discoverSmallShaow,contentMode: .scaleToFill)
- return imageView
- }()
-
- lazy var textLabel: UILabel = {
- let textLabel = UILabel.createLabel(font: .font(size: 14,weight: .medium),textColor: .white)
- return textLabel
- }()
-
- override func creatUI() {
-
- bgContentView.addSubview(imageView)
- bgContentView.addSubview(shadowView)
- bgContentView.addSubview(textLabel)
-
- imageView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- shadowView.snp.makeConstraints { make in
- make.height.equalTo(40)
- make.leading.trailing.bottom.equalToSuperview()
- }
-
- textLabel.snp.makeConstraints { make in
- make.leading.equalTo(8)
- make.trailing.bottom.equalTo(-8)
- }
- }
- }
- class TSDiscoverStyleMoreAnimationCellCell: TSDiscoverStyleMoreBaseCell {
- override var model:TSDiscoverItemModel? {
- didSet {
- guard let model = model else { return }
- textLabel.text = model.name
-
- if let animationModel = model.viewModel as? TSDiscoverAnimationItemVM {
- var images = [UIImage]()
- for imageNamed in animationModel.imageNameds {
- if let image = UIImage(named: imageNamed) {
- images.append(image)
- }
- }
- comparisonView.images = images
- // comparisonView.startAnimation(direction: .rightToLeft)
- }
- }
- }
- lazy var comparisonView: TSImagesComparisonView = {
- let comparisonView = TSImagesComparisonView(frame: CGRect(x: 0, y: 300, width: 300, height: 400))
- comparisonView.isRunloop = true
- comparisonView.cornerRadius = CellCornerRadius
- return comparisonView
- }()
-
- override func creatUI() {
- super.creatUI()
- imageView.addSubview(comparisonView)
- comparisonView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- kMainAsync {
- self.comparisonView.startAnimation(direction: .rightToLeft)
- }
- }
- }
- import Kingfisher
- class TSDiscoverStyleMoreAnimationGifCell: TSDiscoverStyleMoreBaseCell {
- override var model:TSDiscoverItemModel? {
- didSet {
- guard let model = model else { return }
- textLabel.text = model.name
-
- if let animationModel = model.viewModel as? TSDiscoverAnimationItemVM,let imageNamed = animationModel.imageNameds.first {
- if let gifURL = Bundle.main.url(forResource: imageNamed, withExtension: "gif") {
- animatedImageView.kf.setImage(with: gifURL, options: [.cacheOriginalImage]){ result in
- switch result {
- case .success(let _):
- self.animatedImageView.startAnimating()
- case .failure(let error):
- print("GIF 加载失败: \(error.localizedDescription)")
- }
- }
- }
- }
- }
- }
- lazy var animatedImageView: AnimatedImageView = {
- let animatedImageView = AnimatedImageView()
- animatedImageView.autoPlayAnimatedImage = false
- return animatedImageView
- }()
-
- override func creatUI() {
- super.creatUI()
- imageView.addSubview(animatedImageView)
- animatedImageView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- }
- }
|