| | |
| | | |
| | | private var answerIndex:IndexPath? //答案的Index |
| | | private var answerCount:Int = 0 //回答计数 |
| | | private var answerIndexs = Set<IndexPath>() //回答过的Index集合 |
| | | private var filterItems = [[Listen1SubModel]]() //此类型特殊,需要数据清理 |
| | | // private var isPlayedAnsterSet = Set<Int>() //已经播放了的 |
| | | |
| | | private lazy var stackView:UIStackView = { |
| | | let sta = UIStackView() |
| | |
| | | collection.contentInset = UIEdgeInsets(top: 33, left: 0, bottom: 0, right: 0) |
| | | collection.register(UINib(nibName: "ListenFight_lesson_4_CCell", bundle: nil), forCellWithReuseIdentifier: "_ListenFight_lesson_4_CCell") |
| | | collection.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header") |
| | | collection.isScrollEnabled = false |
| | | return collection |
| | | }() |
| | | |
| | |
| | | } |
| | | |
| | | func restore(){ |
| | | |
| | | answerIndexs.removeAll() |
| | | answerCount = 0 |
| | | answerIndex = nil |
| | | for subView in view.subviews{ |
| | |
| | | for i in 0...2{ |
| | | let answerView = Lesson_4_AnswerView.jq_loadNibView() |
| | | answerView.btn_choose.tag = 10 + i |
| | | answerView.tag = 20 + i |
| | | answerView.voiceUrl = tempVoiceArray[i] |
| | | answerView.btn_isAnswer.setImage(viewModel.selectIndex.value?.row == 1 ? UIImage(named: "icon_question"):UIImage(named: "icon_answer"), for: .normal) |
| | | answerView.btn_choose.addTarget(self, action: #selector(answerAction), for: .touchUpInside) |
| | | answerView.alpha = 0 |
| | | answerView.snp.makeConstraints { make in |
| | | make.width.equalTo(221) |
| | | make.height.equalTo(52) |
| | | } |
| | | answerView.btn_choose.isEnabled = false |
| | | answerView.playAt { index in |
| | | answerView.btn_choose.isEnabled = true |
| | | } |
| | | |
| | | UIView.animate(withDuration: 0.05 + Double(i)) { |
| | |
| | | } |
| | | |
| | | @objc func answerAction(btn:UIButton){ |
| | | |
| | | var islistenDone:Int = 0 |
| | | for v in stackView.arrangedSubviews as! [Lesson_4_AnswerView]{ |
| | | if v.btn_choose.isEnabled == true{ |
| | | islistenDone += 1 |
| | | } |
| | | } |
| | | |
| | | if islistenDone != 3{ |
| | | alertError(msg: "请先听完");return |
| | | } |
| | | |
| | | guard let selectIndex = viewModel.selectIndex.value else{ |
| | | alertError(msg: "请先听题");return |
| | |
| | | if tempSubV?.voiceUrl == answerModel?.correct{ |
| | | answerType = .success |
| | | voicePlayer.playSuccessVoice() |
| | | answerCount += 1 |
| | | rootViewModel.correctNum += 1 |
| | | let v = rootViewModel.answerCount.value + 1 |
| | | rootViewModel.answerCount.accept(v) |
| | | DispatchQueue.main.asyncAfter(deadline: .now()+2) { |
| | | self.voicePlayer.playerAt(url: tempSubV!.voiceUrl) |
| | | } |
| | | |
| | | //防止重复答题造成计数错误的问题 |
| | | if !answerIndexs.contains(answerIndex!){ |
| | | answerCount += 1 |
| | | rootViewModel.correctNum += 1 |
| | | let v = rootViewModel.answerCount.value + 1 |
| | | rootViewModel.answerCount.accept(v) |
| | | } |
| | | |
| | | }else{ |
| | | answerType = .fail |
| | | voicePlayer.playFailVoice() |
| | | rootViewModel.errorNum += 1 |
| | | } |
| | | voicePlayer.playerEnd() |
| | | answerIndexs.insert(answerIndex!) |
| | | |
| | | switch answerType { |
| | | case .success: |
| | |
| | | if let copyView = tempSubV?.copyView(){ |
| | | tempSubV?.alpha = 0 |
| | | let newRect = tempSubV!.view_handle.convert(tempSubV!.bounds, to: self.view) |
| | | copyView.frame = CGRect(origin: newRect.origin, size: CGSize(width: 152, height: 52)) |
| | | copyView.frame = CGRect(origin: newRect.origin, size: CGSize(width: 159, height: 52)) |
| | | copyView.view_state.isHidden = true |
| | | copyView.isCopy = true |
| | | copyView.btn_isAnswer.setImage(self.viewModel.selectIndex.value?.row == 1 ? UIImage(named: "icon_question"):UIImage(named: "icon_answer"), for: .normal) |
| | | copyView.img_play.alpha = 1 |
| | | copyView.voiceUrl = tempSubV!.voiceUrl |
| | | self.view.addSubview(copyView) |
| | | self.view.layoutIfNeeded() |
| | | |
| | |
| | | |
| | | self.collectionView.reloadData() |
| | | UIView.animate(withDuration: 0.4) { |
| | | copyView.frame = CGRect(origin: newRect1.origin, size: CGSize(width: 152, height: 52)) |
| | | copyView.frame = CGRect(origin: newRect1.origin, size: CGSize(width: 159, height: 52)) |
| | | } completion: { _ in |
| | | self.viewModel.selectIndex.accept(nil) |
| | | for v in self.stackView.subviews{ |
| | |
| | | self.stackView.layoutIfNeeded() |
| | | |
| | | //回答完成,下一答题 |
| | | if self.answerCount == 2{ |
| | | if self.answerIndexs.count == 2{ |
| | | let v = self.rootViewModel.answerCount.value + 1 |
| | | self.rootViewModel.answerCount.accept(v) |
| | | self.rootViewModel.answerItems[self.page] = self.listenNewModel.subjectList[self.page] |
| | |
| | | } |
| | | |
| | | extension HomeListenFight_lesson_4_VC:UICollectionViewDelegate{ |
| | | func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
| | | // viewModel.selectIndex.accept(indexPath) |
| | | } |
| | | |
| | | } |
| | | |
| | | extension HomeListenFight_lesson_4_VC:UICollectionViewDelegateFlowLayout{ |
| | |
| | | cell.img_state.alpha = 0 |
| | | cell.img_state.transform = .init(scaleX: 0.1, y: 0.1) |
| | | } |
| | | |
| | | if indexPath.row == 1{ |
| | | cell.btn_handle.setImage(UIImage(named: "icon_answer"), for: .normal) |
| | | cell.view_handle.isHidden = true |
| | | }else{ |
| | | cell.btn_handle.setImage(UIImage(named: "icon_question"), for: .normal) |
| | | cell.view_handle.isHidden = true |
| | | } |
| | | |
| | | //问题 |
| | | if filterItems[indexPath.section][indexPath.row].isQuestion == 0{ |
| | | cell.btn_handle.setImage(UIImage(named: "icon_question"), for: .normal) |
| | | cell.view_handle.isHidden = true |
| | | }else{ |
| | | cell.view_handle.isHidden = false |
| | | } |
| | | cell.setModel(m) |
| | | |
| | | |
| | | if answerIndexs.count == 0 && indexPath.section == 1{ |
| | | cell.view_handle.backgroundColor = .gray.withAlphaComponent(0.5) |
| | | cell.btn_play.isEnabled = false |
| | | }else{ |
| | | cell.view_handle.backgroundColor = UIColor(hexString: "#41A2EB") |
| | | cell.btn_play.isEnabled = true |
| | | } |
| | | |
| | | return cell |
| | | } |
| | | |
| | |
| | | |
| | | extension HomeListenFight_lesson_4_VC:VoicePlayerDelegate{ |
| | | func playComplete() { |
| | | |
| | | self.view.isUserInteractionEnabled = true |
| | | } |
| | | |
| | | func playing() { |
| | | |
| | | self.view.isUserInteractionEnabled = false |
| | | } |
| | | } |