杨锴
2024-11-05 0fb7413df54760ac6bd15b90b738e0706de1629e
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
//
//  HomeTopMenuView.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/12.
//
 
import UIKit
import JQTools
import RxSwift
 
enum ImageFromType {
                case local
                case url
}
 
struct HomeTopMenuItem{
                var id = 0
                var title = ""
                var image = ""
                var imageFrom:ImageFromType = .local
}
 
class HomeTopMenuView: UIView,JQNibView{
 
                @IBOutlet weak var image_top: UIImageView!
                @IBOutlet weak var menu_collectionView: UICollectionView!
                @IBOutlet weak var menu_height: NSLayoutConstraint!
                
                private var disposeBag = DisposeBag()
                private var defaultItems = [HomeTopMenuItem]()
                private var clouse:((HomeTopMenuItem)->Void)?
 
                override func awakeFromNib() {
                                super.awakeFromNib()
 
                                menu_height.constant = 119
                                menu_collectionView.delegate = self
                                menu_collectionView.dataSource = self
                                menu_collectionView.isScrollEnabled = false
                                menu_collectionView.register(UINib(nibName: "HomeTopMenuCCell", bundle: nil), forCellWithReuseIdentifier: "_HomeTopMenuCCell")
 
                                defaultItems.append(HomeTopMenuItem(title: "睡眠疗愈", image: "icon_sleep", imageFrom: .local))
                                defaultItems.append(HomeTopMenuItem(title: "高频疗愈", image: "icon_band", imageFrom: .local))
                                defaultItems.append(HomeTopMenuItem(title: "清业疗愈", image: "icon_quiet", imageFrom: .local))
                                defaultItems.append(HomeTopMenuItem(title: "亲子疗愈", image: "icon_parent-child", imageFrom: .local))
                                menu_collectionView.reloadData()
                }
 
                func resetTopImage(){
                                if let imgUrl = UserDefaultSettingViewModel.getSetting()?.bgm?.imageUrl.jq_urlEncoded(){
                                                image_top.sd_setImage(with: URL(string: imgUrl))
                                }
                }
 
                func resetItems(_ items:[HomeTopMenuItem]){
                                defaultItems.removeAll()
                                defaultItems = items
                                menu_collectionView.reloadData()
                }
 
                func insertOthers(_ others:[HomeTopMenuItem]){
                                defaultItems.append(contentsOf: others)
                                menu_collectionView.reloadData()
                }
 
 
                func clickItemAt(_ clouse: @escaping (HomeTopMenuItem)->Void){
                                self.clouse = clouse
                }
}
 
 
extension HomeTopMenuView:UICollectionViewDelegate & UICollectionViewDataSource{
 
                func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                                let item = defaultItems[indexPath.row]
                                clouse?(item)
                }
 
 
                func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                return defaultItems.count
                }
                
                func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeTopMenuCCell", for: indexPath) as! HomeTopMenuCCell
                                cell.setItem(defaultItems[indexPath.row])
                                cell.view_line.isHidden = indexPath.row == 3
                                return cell
                }
}
 
extension HomeTopMenuView:UICollectionViewDelegateFlowLayout{
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
                                return 0
                }
 
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
                                return 0
                }
 
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
                                let w = JQ_ScreenW / 4.0
                                return CGSize(width: w, height: 119)
                }
}