fix
无故事王国
2024-06-18 c1862d736587c9a5c10a368dabaeb72be2df4bcb
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
//  HomeListenGame_2_VC.swift
//  DolphinEnglishLearnStudent
//
//  Created by 无故事王国 on 2024/5/28.
//
 
import UIKit
 
struct Game_2_SelectModel:Hashable{
                var indexPath:IndexPath!
                var model:SimpleListenDataModel!
}
 
class HomeListenGame_2_VC: BaseVC {
 
                private var viewModel = FightAnswerViewModel()
                var rootViewModel:HomeListenFightViewModel!
                private var listen1Model:Listen1Model!
                private var selectModels = [Game_2_SelectModel]()
 
                private var datas = [SimpleListenDataModel]()
 
                private var currentPayCellIndex:IndexPath?
 
                private lazy var label_time:UILabel = {
                                let label = UILabel()
                                label.textColor = UIColor(hexStr: "#EE1111")
                                label.text = "0s"
                                label.font = UIFont.init(name: "Impact", size: 36)
                                return label
 
                }()
 
                private lazy var collectionView:UICollectionView = {
                                let flowLayout = UICollectionViewFlowLayout()
                                let w = (JQ_ScreenW - 40 * 2 - 50) / 5.0
                                flowLayout.itemSize = CGSize(width: w, height: w * 0.675)
 
                                flowLayout.minimumInteritemSpacing = 10
                                flowLayout.minimumLineSpacing = 20
                                flowLayout.scrollDirection = .vertical
                                let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
                                collection.contentInset = UIEdgeInsets(top: 101, left: 40, bottom: 0, right: 40)
                                collection.register(ListenFight_Game_Pocket_CCell.self, forCellWithReuseIdentifier: "_ListenFight_Game_Pocket_CCell")
                                return collection
                }()
 
                private var timer:Timer?
                private var times:Int = 600
                private var voicePlayer = VoicePlayer.share()
 
                required init(listen1Model:Listen1Model){
                                super.init(nibName: nil, bundle: nil)
                                self.listen1Model = listen1Model
 
                                for v in listen1Model.photoList{
                                                v.type = 1 // 图片标识
                                }
 
                                for v in listen1Model.voiceList{
                                                v.type = 2 // 音频标识
                                }
 
                                datas.append(contentsOf: listen1Model.photoList)
                                datas.append(contentsOf: listen1Model.voiceList)
                                datas.shuffle()
                }
 
                required init?(coder: NSCoder) {
                                fatalError("init(coder:) has not been implemented")
                }
 
                override func viewDidAppear(_ animated: Bool) {
                                super.viewDidAppear(animated)
                                voicePlayer.delegate = self
                }
 
                override func viewDidDisappear(_ animated: Bool) {
                                super.viewDidDisappear(animated)
                                voicePlayer.delegate = nil
                                timer?.invalidate()
                                voicePlayer.playerInterrupt()
                }
 
                override func viewDidLoad() {
                                super.viewDidLoad()
 
                                startTimer()
                }
 
                override func setUI() {
                                super.setUI()
 
                                let label_surplusTitle = UILabel()
                                label_surplusTitle.textColor = UIColor.black
                                label_surplusTitle.text = "答题剩余时间:"
                                label_surplusTitle.font = .systemFont(ofSize: 14, weight: .medium)
 
                                view.addSubview(label_surplusTitle)
                                label_surplusTitle.snp.makeConstraints { make in
                                                make.left.equalTo(40)
                                                make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(46)
                                                make.height.equalTo(20)
                                }
 
                                view.addSubview(label_time)
                                label_time.snp.makeConstraints { make in
                                                make.left.equalTo(label_surplusTitle.snp.right).offset(0)
                                                make.centerY.equalTo(label_surplusTitle)
                                                make.height.equalTo(44)
                                }
 
 
                                collectionView.delegate = self
                                collectionView.dataSource = self
                                collectionView.showsVerticalScrollIndicator = false
                                collectionView.jq_addShadows(shadowColor: UIColor.black.withAlphaComponent(0.1), corner: 8, radius: 10, offset: CGSize(width: 0, height: 2), opacity: 1)
                                collectionView.backgroundColor = .clear
                                view.addSubview(collectionView)
                                collectionView.snp.makeConstraints { make in
                                                make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
                                                make.left.right.equalToSuperview()
                                                make.bottom.equalToSuperview()
                                }
 
                                view.layoutIfNeeded()
                }
 
                private func startTimer(){
                                if timer == nil{
                                                timer = Timer(timeInterval: 1.0, target: self, selector: #selector(runloopTime), userInfo: nil, repeats: true)
                                }
                                timer?.fire()
                                RunLoop.current.add(timer!, forMode: .default)
                }
 
                @objc private func runloopTime(){
                                times -= 1
                                label_time.text = "\(times)s"
 
                                if times == 0{
                                                timer?.fireDate = .distantFuture
                                                DispatchQueue.main.asyncAfter(deadline: .now()+3) {
                                                                self.times = 10
                                                                self.timer?.fireDate = .distantPast
                                                }
                                }
                }
 
 
                private func nextQuesAction(){
 
                }
 
                private func answerQuestion(){
                                view.layoutIfNeeded()
                                var answerType:Fight_lessonType = .none
                                answerType = .success
 
                                switch answerType {
                                                case .success:
                                                                voicePlayer.playSuccessVoice()
                                                                viewModel.answerType.accept(.success)
                                                                collectionView.reloadData()
 
                                                case .fail:
                                                                voicePlayer.playFailVoice()
                                                                viewModel.answerType.accept(.fail)
                                                                collectionView.reloadData()
                                                                DispatchQueue.main.asyncAfter(wallDeadline: .now()+2.5) {
                                                                                self.viewModel.answerType.accept(.none)
                                                                                self.collectionView.reloadData()
                                                                }
                                                case .none:
                                                                break
                                }
                }
}
 
extension HomeListenGame_2_VC:UICollectionViewDelegate{
                func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
 
                                if datas[indexPath.row].isOpen{
                                                print("已经展开过了");return
                                }
 
                                if selectModels.last?.indexPath == indexPath{
                                                print("重复点击");return
                                }
 
                                let model = datas[indexPath.row]
 
                                if selectModels.count >= 2{
                                                selectModels.removeFirst()
                                }
 
                                selectModels.append(Game_2_SelectModel(indexPath: indexPath, model: model))
 
                                print(selectModels.map({"\($0.indexPath.row)"}).joined(separator: "——"))
 
                                if selectModels.count == 1{
                                                let cell = self.collectionView.cellForItem(at: indexPath) as! ListenFight_Game_Pocket_CCell
                                                cell.toFromAction()
                                }
 
                                if selectModels.count == 2{
                                                let cell = self.collectionView.cellForItem(at: indexPath) as! ListenFight_Game_Pocket_CCell
                                                cell.toFromAction()
 
                                                let firstM = selectModels.first
                                                let lastM = selectModels.last
 
                                                if firstM != nil && lastM != nil{
                                                                if firstM!.model.id == lastM!.model.id{
                                                                                firstM!.model.isOpen = true
                                                                                lastM?.model.isOpen = true
                                                                                selectModels.removeAll()
                                                                                print("相同")
                                                                                rootViewModel.correctNum += 1
                                                                }else{
                                                                                print("不相同")
                                                                                rootViewModel.errorNum += 1
                                                                                let firstIndex = firstM!.indexPath
                                                                                let secondIndex = lastM!.indexPath
 
                                                                                let firstIndexCell = self.collectionView.cellForItem(at: firstIndex!) as! ListenFight_Game_Pocket_CCell
                                                                                let secondIndexCell = self.collectionView.cellForItem(at: secondIndex!) as! ListenFight_Game_Pocket_CCell
 
                                                                                DispatchQueue.main.asyncAfter(deadline: .now()+0.5){
                                                                                                firstIndexCell.toBackAction()
                                                                                                secondIndexCell.toBackAction()
                                                                                                self.selectModels.removeAll()
                                                                                }
                                                                }
                                                }
 
                                                let surplusListCount = datas.filter({$0.isOpen == false}).count
                                                if surplusListCount == 0{
                                                                NotificationCenter.default.post(name: NextLession_Noti, object: ["gameId":listen1Model.data!.id,"gameIntegral":listen1Model.data!.answerIntegral])
                                                                rootViewModel.answerItems[0] = self.listen1Model
                                                                timer?.invalidate()
                                                }
                                                print("剩余:\(surplusListCount)")
 
                                }
                }
}
 
extension HomeListenGame_2_VC:UICollectionViewDataSource{
                func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                                let model = datas[indexPath.row]
                                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_Game_Pocket_CCell", for: indexPath) as! ListenFight_Game_Pocket_CCell
                                cell.indexPath = indexPath
                                cell.setSimpleModel(model)
                                cell.cellPayatIndex {[weak self] index in
                                                self?.currentPayCellIndex = index
                                }
                                return cell
                }
 
                func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                return datas.count
                }
}
 
extension HomeListenGame_2_VC: UICollectionViewDelegateFlowLayout {
 
                // 实现以下方法以支持转场动画
                func collectionView(_ collectionView: UICollectionView, transitionLayoutForOldLayout fromLayout: UICollectionViewLayout, newLayout toLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout {
                                return UICollectionViewTransitionLayout(currentLayout: fromLayout, nextLayout: toLayout)
                }
 
                // 实现以下方法以返回自定义的转场动画控制器
                func collectionView(_ collectionView: UICollectionView, transitionAnimationControllerForOldCell: UICollectionViewCell, newCell: UICollectionViewCell) -> UIViewControllerAnimatedTransitioning? {
                                return FlipAnimationController()
                }
 
                // FlipAnimationController实现转场动画的具体细节
                class FlipAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
                                func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
                                                return 0.3
                                }
 
                                func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
                                                guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else { return }
                                                guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else { return }
                                                let containerView = transitionContext.containerView
 
                                                // 初始化动画参数
                                                toView.frame = transitionContext.finalFrame(for: transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!)
                                                containerView.addSubview(toView)
 
                                                // 翻牌动画
                                                UIView.transition(from: fromView, to: toView, duration: transitionDuration(using: transitionContext), options: [.transitionFlipFromLeft]) { completed in
                                                                transitionContext.completeTransition(completed)
                                                }
                                }
                }
}
 
extension HomeListenGame_2_VC:VoicePlayerDelegate{
                func playComplete() {
                                if let index = currentPayCellIndex{
                                                let cell = collectionView.cellForItem(at: index) as! ListenFight_Game_Pocket_CCell
                                                cell.cellResotePay()
                                }
                }
 
                func playing() {
                                
                }
 
}