add
无故事王国
2024-05-28 64d4343b54e747804656a3fb7eca4a2d14d6e5de
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
//  HomeListenFight_lesson_1_VC.swift
//  DolphinEnglishLearnStudent
//
//  Created by 无故事王国 on 2024/5/24.
//
 
import UIKit
import JQTools
import RxRelay
 
class FightAnswerViewModel{
                var selectIndex = BehaviorRelay<IndexPath?>(value: nil)
                var answerType = BehaviorRelay<Fight_lessonType>(value: .none)
}
 
/// 题目类型一
class HomeListenFight_lesson_1_VC: BaseVC {
 
                private var viewModel = FightAnswerViewModel()
 
                private lazy var menuView:StudyHandleView = {
                                let menu = StudyHandleView.jq_loadNibView()
                                menu.listenType = .lesson1
                                return menu
                }()
 
                private lazy var collectionView:UICollectionView = {
                                let flowLayout = UICollectionViewFlowLayout()
                                let w = (JQ_ScreenW - 194 * 2 - 25) / 2
                                flowLayout.itemSize = CGSize(width: w, height: w * 0.745)
                                flowLayout.minimumLineSpacing = 25
                                flowLayout.minimumInteritemSpacing = 25
                                flowLayout.scrollDirection = .vertical
                                let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
                                collection.register(UINib(nibName: "ListenFight_lesson_1_CCell", bundle: nil), forCellWithReuseIdentifier: "_ListenFight_lesson_1_CCell")
                                return collection
                }()
 
                override func viewDidLoad() {
                                super.viewDidLoad()
                                navigationItem.titleView = UIView()
                }
 
                override func setUI() {
                                super.setUI()
 
                                collectionView.delegate = self
                                collectionView.dataSource = self
                                collectionView.backgroundColor = UIColor(hexStr: "#C3BFB3")
                                view.addSubview(collectionView)
                                collectionView.snp.makeConstraints { make in
                                                make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(101)
                                                make.left.equalTo(194)
                                                make.right.equalTo(-194)
                                                make.bottom.equalToSuperview()
                                }
 
                                view.addSubview(menuView)
                                menuView.snp.makeConstraints { make in
                                                make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(24)
                                                make.centerX.equalToSuperview()
                                                make.height.equalTo(52)
                                                make.width.equalTo(199)
                                }
                }
 
                override func setRx() {
                                viewModel.selectIndex.subscribe(onNext: {[weak self] index in
 
                                                guard let index = index else { return }
 
                                                //判断选中是否正确逻辑
 
                                                if let cell = self?.collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_1_CCell", for: index) as? ListenFight_lesson_1_CCell{
                                                                var answer:Fight_lessonType = .none
 
                                                                answer = .success
 
                                                                switch answer {
                                                                                case .success:
                                                                                                self?.viewModel.answerType.accept(.success)
                                                                                                self?.answerSuccess(cell)
                                                                                case .fail:
                                                                                                self?.viewModel.answerType.accept(.fail)
                                                                                                self?.answerFail()
                                                                                default:break
                                                                }
                                                }
                                }).disposed(by: disposeBag)
                }
 
                //回答正确
                private func answerSuccess(_ cell:ListenFight_lesson_1_CCell){
                                menuView.snp.removeConstraints()
                                collectionView.isUserInteractionEnabled = false
                                let v = cell.view_topHandle.convert(cell.bounds, to: self.view)
                                UIView.animate(withDuration: 0.3) {
                                                self.menuView.snp.updateConstraints { make in
                                                                make.top.equalTo(self.view).offset(v.origin.y + UIDevice.jq_safeEdges.top + 101 + 50)
                                                                make.left.equalToSuperview().offset(v.origin.x + 194)
                                                                make.width.equalTo(v.size.width + 50)
                                                                make.height.equalTo(40)
                                                }
                                                self.view.layoutIfNeeded()
                                }completion: { _ in
                                                self.collectionView.reloadData()
                                                DispatchQueue.main.asyncAfter(deadline: .now()+2){
                                                                NotificationCenter.default.post(name: NextLession_Noti, object: nil)
                                                                self.collectionView.isUserInteractionEnabled = true
                                                                self.viewModel.answerType.accept(.none)
                                                                self.viewModel.selectIndex.accept(nil)
                                                }
                                }
                }
 
                private func answerFail(){
                                self.collectionView.reloadData()
                }
}
 
extension HomeListenFight_lesson_1_VC:UICollectionViewDelegate{
                func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                                viewModel.selectIndex.accept(indexPath)
                }
}
 
extension HomeListenFight_lesson_1_VC:UICollectionViewDataSource{
                func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_1_CCell", for: indexPath) as! ListenFight_lesson_1_CCell
                                cell.jq_addShadows(shadowColor: .black.withAlphaComponent(0.31), corner: 5, radius: 5, offset: CGSize(width: 0, height: 1), opacity: 1)
                                cell.backgroundColor = .white
 
                                if viewModel.selectIndex.value == indexPath{
                                                cell.setState(state: viewModel.answerType.value)
                                }else{
                                                cell.setState(state: .none)
                                }
                                return cell
                }
 
                func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                return 4
                }
}