杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
//  Home_Style_3_TCell.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/12.
//
 
import UIKit
import JQTools
import RxSwift
 
class Home_Style_3_TCell: UITableViewCell {
 
                @IBOutlet weak var collectionView: UICollectionView!
                var meditationModels = [MeditationModel]()
    private var showType:DisplayType!
    private let disposeBag = DisposeBag()
 
                override func awakeFromNib() {
        super.awakeFromNib()
                                selectionStyle = .none
 
                                collectionView.collectionViewLayout = TestLeftRightCollectionViewFlowLayout(width: JQ_ScreenW, height: 213.5)
                                collectionView.delegate = self
                                collectionView.dataSource = self
                                collectionView.isPagingEnabled = false
                                collectionView.bounces = false
                                collectionView.showsHorizontalScrollIndicator = false
                                collectionView.register(UINib(nibName: "HomeRelaxBannerCCell", bundle: nil), forCellWithReuseIdentifier: "_HomeRelaxBannerCCell")
                                collectionView.contentInset = UIEdgeInsets(top: 0, left: 100 , bottom: 0, right: 100)
 
    }
 
                func setModels(_ items:[MeditationModel],showType: DisplayType){
        self.showType = showType
                                self.meditationModels = items
 
//        if items.count >= 3 {
 
//        }
 
        collectionView.reloadData()
 
        DispatchQueue.main.asyncAfter(delay: 0.1) {
            let centerIndex = ceil(Double(Int(1000 / items.count)))
            self.collectionView.scrollToItem(at: IndexPath(row: Int(centerIndex), section: 0), at: .centeredHorizontally, animated: false)
        }
                }
}
 
extension Home_Style_3_TCell:UICollectionViewDelegate & UICollectionViewDataSource{
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
 
        print("--->\(collectionView.visibleCells)")
 
        var showAtCell:UICollectionViewCell?
        if collectionView.visibleCells.count == 2{
            showAtCell = collectionView.visibleCells.last
        }
 
        if collectionView.visibleCells.count == 3{
            showAtCell = collectionView.visibleCells[1]
        }
 
        if let cell = showAtCell,let indexPath = collectionView.indexPath(for: cell){
            let realIndex = indexPath.row % 1000
            collectionView.scrollToItem(at: IndexPath(row: realIndex, section: 0), at: .centeredHorizontally, animated: false)
 
        }
 
//        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
    }
 
                func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                return meditationModels.count * 1000
                }
                
                func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                                 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeRelaxBannerCCell", for: indexPath) as! HomeRelaxBannerCCell
        let realIndex = indexPath.item % meditationModels.count
        cell.setMeditationModel(meditationModels[realIndex],showType: showType)
                                return cell
                }
 
                func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
 
        let index = indexPath.item % meditationModels.count
 
        let item = meditationModels[index]
 
        if (item.chargeType == .payment || item.chargeType == .vipFree || item.id == 0) && (UserViewModel.getLoginInfo()?.token.isEmpty ?? true){
            sceneDelegate?.checkisLoginState()
            return
        }
 
        Services.getMeditationDetail(id: item.id).subscribe(onNext: {[weak self]data in
            guard let weakSelf = self else { return }
            if let m = data.data{
                let isVip = m.isVip == .yes
                if m.chargeType == .free || (isVip && m.chargeType == .vipFree) || (m.chargeType == .payment && m.isBuy == .yes){
                    let vc = HomeItemDetailVC(model: m)
                    JQ_currentViewController().jq_push(vc: vc)
                    return
                }
 
                if m.chargeType == .payment && m.isBuy == .no{
                    guard sceneDelegate!.checkisLoginState() else{return}
                    let vc = PaymentOrderVC(museItemModel: m,type: .muse,showType: weakSelf.showType)
                    JQ_currentViewController().jq_push(vc: vc)
                    return
                }
 
                if m.chargeType == .vipFree{
                    let vc = VIPCenterVC()
                    JQ_currentViewController().jq_push(vc: vc)
                    return
                }
            }
        }).disposed(by: self.disposeBag)
                }
}
 
extension Home_Style_3_TCell:UICollectionViewDelegateFlowLayout{
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
                                return CGSize(width: 148.25, height: 213.45)
                }
}