| | |
| | | |
| | | import UIKit |
| | | |
| | | let Games_1_Refresh_Noti = Notification.Name.init("Games_1_Refresh_Noti") |
| | | |
| | | class HomeListenGame_1_VC: BaseVC { |
| | | |
| | | private var viewModel = FightAnswerViewModel() |
| | | private var listen1Model:Listen1Model! |
| | | |
| | | var rootViewModel:HomeListenFightViewModel! |
| | | |
| | | private var totalCount:Int = 1 //游戏的总数量 |
| | | |
| | | private lazy var label_class:UILabel = { |
| | | let label = UILabel() |
| | |
| | | label.textColor = UIColor(hexStr: "#EE1111") |
| | | label.text = "请在10s内选择答案!" |
| | | label.textAlignment = .center |
| | | label.isHidden = true |
| | | label.font = .systemFont(ofSize: 14, weight: .medium) |
| | | return label |
| | | }() |
| | |
| | | }() |
| | | |
| | | private var timer:Timer? |
| | | private var times:Int = 10 |
| | | private var times:Int = 60 |
| | | |
| | | private var answerSet = Set<Listen1SubModel>() |
| | | private var currentAnswer:Listen1SubModel?{ |
| | | didSet{ |
| | | if let v = currentAnswer{ |
| | | view_studyHandleView.vioceSoundUrl = v.correct |
| | | view_studyHandleView.voicePlayer.playerAt(url: v.correct) |
| | | } |
| | | } |
| | | } |
| | | |
| | | required init(listen1Model:Listen1Model){ |
| | | super.init(nibName: nil, bundle: nil) |
| | | self.listen1Model = listen1Model |
| | | } |
| | | |
| | | required init?(coder: NSCoder) { |
| | | fatalError("init(coder:) has not been implemented") |
| | | } |
| | | |
| | | override func viewDidLoad() { |
| | | super.viewDidLoad() |
| | | |
| | | ChooseLevelView.show { level in |
| | | GameBeginTipView.show { |
| | | self.startTimer() |
| | | if listen1Model != nil{ |
| | | times = listen1Model?.data?.time ?? 10 |
| | | collectionView.reloadData() |
| | | label_hint.isHidden = false |
| | | label_hint.text = "\(totalCount)" |
| | | if timer == nil{startTimer()} |
| | | |
| | | for v in listen1Model?.subjectList ?? []{ |
| | | answerSet.insert(v) |
| | | } |
| | | print("--->开始答题:剩余:\(answerSet.count)") |
| | | currentAnswer = answerSet.randomElement() //随机 |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | view_class_title.addSubview(label_class) |
| | | label_class.text = "\(totalCount)" |
| | | label_class.snp.makeConstraints { make in |
| | | make.left.equalTo(11) |
| | | make.right.equalTo(-12) |
| | |
| | | view.layoutIfNeeded() |
| | | } |
| | | |
| | | private func startTimer(){ |
| | | |
| | | func startTimer(){ |
| | | if timer == nil{ |
| | | timer = Timer(timeInterval: 1.0, target: self, selector: #selector(runloopTime), userInfo: nil, repeats: true) |
| | | } |
| | |
| | | |
| | | if times == 0{ |
| | | timer?.fireDate = .distantFuture |
| | | DispatchQueue.main.asyncAfter(deadline: .now()+3) { |
| | | self.times = 10 |
| | | self.timer?.fireDate = .distantPast |
| | | if let c = currentAnswer{ |
| | | answerSet.remove(c) |
| | | } |
| | | currentAnswer = answerSet.randomElement() //随机 |
| | | times = listen1Model?.data?.time ?? 0 |
| | | timer?.fireDate = .distantPast |
| | | totalCount += 1 |
| | | label_class.text = "\(totalCount)" |
| | | |
| | | //答题完成 |
| | | if self.answerSet.count == 0{completeQuestion()} |
| | | } |
| | | } |
| | | |
| | | private func nextQuesAction(){ |
| | | |
| | | } |
| | | |
| | | private func answerQuestion(){ |
| | | view.layoutIfNeeded() |
| | | var answerType:Fight_lessonType = .none |
| | | answerType = .success |
| | | |
| | | guard let row = viewModel.selectIndex.value?.row else { alertError(msg: "请选择");return } |
| | | |
| | | var answerType:Fight_lessonType = .none |
| | | |
| | | if currentAnswer?.id == listen1Model?.subjectList[row].id{ |
| | | answerType = .success |
| | | }else{ |
| | | answerType = .fail |
| | | } |
| | | switch answerType { |
| | | case .success: |
| | | viewModel.answerType.accept(.success) |
| | | collectionView.reloadData() |
| | | |
| | | rootViewModel.correctNum += 1 |
| | | if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_Game_CCell", for: viewModel.selectIndex.value!) as? ListenFight_Game_CCell{ |
| | | let newRect = cell.contentView.convert(cell.bounds, from: self.collectionView) |
| | | let x = abs(newRect.origin.x) + self.collectionView.contentInset.left + 5 |
| | |
| | | DispatchQueue.main.asyncAfter(deadline: .now()+0.5) { |
| | | self.viewModel.answerType.accept(.none) |
| | | self.collectionView.reloadData() |
| | | self.times = 11 |
| | | self.nextQuesAction() |
| | | self.times = self.listen1Model?.data?.time ?? 10 |
| | | self.totalCount += 1 |
| | | self.label_class.text = "\(self.totalCount)" |
| | | self.answerSet.remove(self.currentAnswer!) |
| | | self.currentAnswer = self.answerSet.randomElement() |
| | | print("--->下一题:\(self.currentAnswer?.id ?? 0) 剩余\(self.answerSet.count) 计数:\(self.totalCount)") |
| | | |
| | | //答题完成 |
| | | if self.answerSet.count == 0{ |
| | | self.completeQuestion() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | case .fail: |
| | | rootViewModel.errorNum += 1 |
| | | viewModel.answerType.accept(.fail) |
| | | collectionView.reloadData() |
| | | DispatchQueue.main.asyncAfter(wallDeadline: .now()+2.5) { |
| | | self.viewModel.answerType.accept(.none) |
| | | self.collectionView.reloadData() |
| | | self.times = 11 |
| | | self.nextQuesAction() |
| | | } |
| | | |
| | | case .none: |
| | | break |
| | | } |
| | | } |
| | | |
| | | private func completeQuestion(){ |
| | | print("答题完成") |
| | | self.label_hint.text = "答题完成" |
| | | self.timer?.invalidate() |
| | | self.rootViewModel.answerItems[0] = self.listen1Model |
| | | NotificationCenter.default.post(name: NextLession_Noti, object: ["gameId":listen1Model.data!.id,"gameIntegral":listen1Model.data!.integral]) |
| | | } |
| | | } |
| | | |
| | | extension HomeListenGame_1_VC:UICollectionViewDelegate{ |
| | | func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
| | | viewModel.selectIndex.accept(indexPath) |
| | | answerQuestion() |
| | | let model = listen1Model!.subjectList[indexPath.row] |
| | | if answerSet.contains(model){ |
| | | viewModel.selectIndex.accept(indexPath) |
| | | answerQuestion() |
| | | } |
| | | } |
| | | } |
| | | |
| | | extension HomeListenGame_1_VC:UICollectionViewDataSource{ |
| | | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| | | let model = listen1Model!.subjectList[indexPath.row] |
| | | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_Game_CCell", for: indexPath) as! ListenFight_Game_CCell |
| | | |
| | | if viewModel.selectIndex.value == indexPath{ |
| | | cell.setState(state: viewModel.answerType.value) |
| | | }else{ |
| | | cell.setState(state: .none) |
| | | } |
| | | |
| | | cell.setModel(model) |
| | | return cell |
| | | } |
| | | |
| | | |
| | | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| | | return 12 |
| | | return listen1Model?.subjectList.count ?? 0 |
| | | } |
| | | } |